Guide to the Spring WebUtils and ServletRequestUtils (#1184)
* rest with spark java * 4 * Update Application.java * indentation changes * spring @requestmapping shortcuts * removing spring requestmapping and pushing spring-mvc-java * Joining/Splitting Strings with Java and Stream API * adding more join/split functionality * changing package name * testcase change * adding webutils
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.utils;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages="com.baeldung.utils")
|
||||
public class Application {
|
||||
|
||||
@RolesAllowed("*")
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.utils.controller;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.bind.ServletRequestUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
@Controller
|
||||
public class UtilsController {
|
||||
|
||||
@GetMapping("/utils")
|
||||
public String webUtils(Model model) {
|
||||
return "utils";
|
||||
}
|
||||
|
||||
@PostMapping("/setParam")
|
||||
public String post(HttpServletRequest request, Model model) {
|
||||
String param = ServletRequestUtils.getStringParameter(request, "param", "DEFAULT");
|
||||
|
||||
// Long param = ServletRequestUtils.getLongParameter(request, "param",1L);
|
||||
// boolean param = ServletRequestUtils.getBooleanParameter(request, "param", true);
|
||||
// double param = ServletRequestUtils.getDoubleParameter(request, "param", 1000);
|
||||
// float param = ServletRequestUtils.getFloatParameter(request, "param", (float) 1.00);
|
||||
// int param = ServletRequestUtils.getIntParameter(request, "param", 100);
|
||||
|
||||
// try {
|
||||
// ServletRequestUtils.getRequiredStringParameter(request, "param");
|
||||
// } catch (ServletRequestBindingException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
WebUtils.setSessionAttribute(request, "parameter", param);
|
||||
model.addAttribute("parameter", "You set: "+(String) WebUtils.getSessionAttribute(request, "parameter"));
|
||||
return "utils";
|
||||
}
|
||||
|
||||
@GetMapping("/other")
|
||||
public String other(HttpServletRequest request, Model model) {
|
||||
String param = (String) WebUtils.getSessionAttribute(request, "parameter");
|
||||
model.addAttribute("parameter", param);
|
||||
return "other";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user