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:
parent
a4c5fb1c12
commit
6b0e59afac
0
core-java/0.004102810554955205
Normal file
0
core-java/0.004102810554955205
Normal file
0
core-java/0.04832801936270381
Normal file
0
core-java/0.04832801936270381
Normal file
0
core-java/0.5633433244738808
Normal file
0
core-java/0.5633433244738808
Normal file
0
core-java/0.6256429734439612
Normal file
0
core-java/0.6256429734439612
Normal file
0
core-java/0.9799201796740292
Normal file
0
core-java/0.9799201796740292
Normal file
@ -12,7 +12,7 @@ import com.baeldung.string.JoinerSplitter;
|
|||||||
public class JoinerSplitterTest {
|
public class JoinerSplitterTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArray_transformedToStream_convertToString() {
|
public void provided_array_convert_to_stream_and_convert_to_string() {
|
||||||
|
|
||||||
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
|
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ public class JoinerSplitterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
|
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
|
||||||
|
|
||||||
String[] programming_languages = {"java", "python",
|
String[] programming_languages = {"java", "python",
|
||||||
"nodejs", "ruby"};
|
"nodejs", "ruby"};
|
||||||
String expectation = "[java,python,nodejs,ruby]";
|
String expectation = "[java,python,nodejs,ruby]";
|
||||||
@ -34,6 +35,7 @@ public class JoinerSplitterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_transformedToStream_convertToList() {
|
public void givenString_transformedToStream_convertToList() {
|
||||||
|
|
||||||
String programming_languages = "java,python,nodejs,ruby";
|
String programming_languages = "java,python,nodejs,ruby";
|
||||||
|
|
||||||
List<String> expectation = new ArrayList<String>();
|
List<String> expectation = new ArrayList<String>();
|
||||||
@ -49,6 +51,7 @@ public class JoinerSplitterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_transformedToStream_convertToListOfChar() {
|
public void givenString_transformedToStream_convertToListOfChar() {
|
||||||
|
|
||||||
String programming_languages = "java,python,nodejs,ruby";
|
String programming_languages = "java,python,nodejs,ruby";
|
||||||
|
|
||||||
List<Character> expectation = new ArrayList<Character>();
|
List<Character> expectation = new ArrayList<Character>();
|
||||||
|
@ -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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
16
spring-boot/src/main/resources/templates/other.html
Normal file
16
spring-boot/src/main/resources/templates/other.html
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8"/>
|
||||||
|
<title>Spring Utils Demo</title>
|
||||||
|
<style type="text/css">
|
||||||
|
.param{
|
||||||
|
color:green;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Parameter set by you: <p th:text="${parameter}" class="param"/>
|
||||||
|
</body>
|
||||||
|
</html>
|
23
spring-boot/src/main/resources/templates/utils.html
Normal file
23
spring-boot/src/main/resources/templates/utils.html
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8"/>
|
||||||
|
<title>Spring Utils Demo</title>
|
||||||
|
<style type="text/css">
|
||||||
|
.param{
|
||||||
|
color:green;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="setParam" method="POST">
|
||||||
|
<h3>Set Parameter: </h3>
|
||||||
|
<p th:text="${parameter}" class="param"/>
|
||||||
|
<input type="text" name="param" id="param"/>
|
||||||
|
<input type="submit" value="SET"/>
|
||||||
|
</form>
|
||||||
|
<br/>
|
||||||
|
<a href="other">Another Page</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user