1, wildcard character JumpSPRING4 Spring MVC Combat (a)-read the spring in action to build the simplest MVC. Only a specific path has been configured.
But if you want to jump after a wildcard match like struts, in spring MVC, this should look at the document.
Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/the original API documentation
There is a URI template method to find: I can think of the jump method is the same.
Get the parameters, check the data and understand, and then feel pretty bad.
The entire map is also available. Don't get a map like struts2 through Actioncontext's getparameters.
And this thing, really is to combine to know what to use.
Visit: http://localhost:8080/project/spittle/3?a=8&b=2
@RequestMapping (value = "/spittle/{ownerid}", method= {requestmethod.get}) public
String spittle (@PathVariable String ownerID, @RequestParam ("a") int petid, @RequestParam map<string,string> allrequestparams) {
SYSTEM.OUT.PRINTLN ("uri parameter" +ownerid); the value of/spittle/{ownerid} ownerid in the URI obtained is 3
System.out.println (" Request Parameter "+petid); Gets the value of parameter A. Here is 8
//entryset return set<map.entry<k,v>> map.entry A map entry is a key value pair, Getkey get Key,getvalue Get value
//Replace Keyset method to return is Set<k>, after the set is traversed, only key is taken, then the value is obtained from map according to key.
for (map.entry<string, string> entry:allRequestParams.entrySet ()) {
System.out.println (entry.getkey () + "->" +entry.getvalue ()); The acquisition of the entire map can be obtained from a->8 b->2
System.out.println (ownerid);
Return "Spittle_" +ownerid;
}
So to do the class wildcard jump, Value = "/spittle/{ownerid}" here according to their own page jump configuration and then return.
2, 405 error
If only the Post method is qualified, then a GET request is reported with a 405 error.
Change to method= {requestmethod.post,requestmethod.get} or directly without qualification.
3, front page display before the more complex control jump, and then can get the URI and front-end request parameters.
The next step is to pass the parameters of the background processing to the front end.
@RequestMapping (value = "/spittle2", method= {requestmethod.post,requestmethod.get}) public
Modelandview ListData2 () throws Exception {
Modelandview model = new Modelandview ("OK");
String json = "{\ total\": 10,\ "rows\": [{\ "a\": 1,\ "b\": \ "Ee\"}]} ";
Model.addobject ("MSG", JSON);
return model;
}
or write directly:
@RequestMapping (value = "/spittle2", method= {requestmethod.post,requestmethod.get}) public
Modelandview ListData2 () throws Exception {return
new Modelandview ("OK", "msg", "{\ total\": 10,\ "rows\": [{\ a\]: 1,\ "b\": \ "ee\ "}]}");
}
Modelandview Constructors
Public Modelandview (Object view, String modelname,object modelobject)
Convenient constructor to take a single model object.
Parameters:
View-view object to render (usually a Servlet MVC View object)
Modelname-name of the single entry in the model
Modelobject-the Single Model Object
Here our view is OK, according to the view parser, we will find/web-inf/views/ok.jsp
At the same time passed model.
At this point, the ok.jsp display msg value is very simple, you need to reference jstl. Use the EL expression display.
web-inf/views/ok.jsp
<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
${msg}
The more complex data display is not to mention, here is a process that introduces the simplest values from the background to the front-end display.
4, with Ajax interaction and 406 error to think of not jump to the page, if not in the page directly display but asynchronous request directly to obtain data. After a constant groping, I found my thinking was wrong. It's totally wrong.
I didn't know what was wrong.
Take a look back at Spring MVC Combat (a)-read "Spring in action" to build the simplest MVC. Steps 4th and 5th in the process.
The information is processed here in the controller (Controller). The package model data also has the view name to Dispatcherservlet.
Dispatcherservlet Consulting View resolver (view parser) to find a specific view map.
@Controller public
class HomeController {
@RequestMapping (value = '/test ') public
String HelloWorld () { return
"HelloWorld";
}
Normally, this is actually a view name that returns to the Hello World, consulting the view parser.
Public Viewresolver Viewresolver () {
Internalresourceviewresolver resolver = new Internalresourceviewresolver ();
Resolver.setprefix ("/web-inf/views/");
Resolver.setsuffix (". jsp");
return resolver;
}
To find/web-inf/views/helloworld.jsp.
But if the AJAX request is not just a request but also wants to get the data from the request, it is not a jump page, but rather a direct
Gets the return data. How does this happen in spring MVC? --@ResponseBody
What's the use of @ResponseBody, it's amazing, the return type is written to the response HTTP body
Yes, the return type is written directly to the HTTP body of response.
What is the equivalent of the concept, you access a URI, the page shows the response return of things, content is the entire HTTP body.
@ResponseBody
@RequestMapping (value = "/test") public
String HelloWorld () {return
' Hello world ';
}
At this point the access is read directly to the response. This is the difference from the page jump.
Http://localhost:8080/project/static/test.html:
If we want to return the JSON type. Spring can return a map type.
@RequestMapping (value = "/spittle", method= {requestmethod.post,requestmethod.get}) public
@ResponseBody Map Listdata () throws Exception {
map<string, object> Map = new hashmap<string, object> ();
Map.put ("A", "a");
Map.put ("B", 2);
return map;
}
Changing the test.html Ajax request url,success Gets the data to get to the JSON string.
But here's a 406 error. Because thinking about it will know that because response directly shows a map such a
object is not possible, so it is not acceptable, but if the string is simple.
Pom.xml Add dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId> jackson-databind</artifactid>
<version>2.4.0</version>
</dependency>
This turns the map into a JSON string. Add Jackson-databind because dependencies jackson-annotations and Jackson-core are also added.
5, the interaction with Easyui. Easyui DataGrid accepts JSON string format as: {"Total": "Rows": [{"A": 1, "B": "EE"}]}
Some people do not display the data correctly and give me a step.
Found the problem today.
Because in the background processing is, some people in order to avoid using escape characters. Change the double quotation marks directly to single quotes. This way to easyui this side is not recognized data.
String json = "[{' id ': 1, ' name ': ' ee ', ' password ': ' 1 '}]";
The DataGrid does not load data at all. Change to escape character normal
String json = "[{\ id\": 1,\ "name\": \ "ee\", \ "password\": \ "1\"}] ";
@RequestMapping (value = "/spittle", method= {requestmethod.post,requestmethod.get}) public
@ResponseBody Map Listdata () throws Exception {
map<string, object> Map = new hashmap<string, object> ();
Map.put ("A", 1);
Map.put ("B", 2);
return map;
}
The returned map will then be converted to: {"Total": "1", "Rows": {"a":, "B": 2}}
If rows have more than one, how to transform: "Total": "" "," Rows ": [{" A ": 1," B ": 2},{" a ": 1," B ": 2}]}
To analyze the array in rows, place the map in the array.
@RequestMapping (value = "/spittle", method= {requestmethod.post,requestmethod.get}) public
@ResponseBody Map Listdata () throws Exception {
map<string, object> bigmap = new hashmap<string, object> ();
map<string, object> map1 = new hashmap<string, object> ();
map<string, object> map2 = new hashmap<string, object> ();
Bigmap.put ("Total", "a");
Map1.put ("A", 1);
Map2.put ("A", 1);
Map1.put ("B", 2);
Map2.put ("B", 2);
Map[] Maparray = {MAP1,MAP2};
Bigmap.put ("Rows", Maparray);
return bigmap;
}
Web-inf/static/test2.html
<!
DOCTYPE html>
Last normal display: