This section focuses on using SPRINGMVC for controller to view, involving code that contains JSP views,
Unlike the largest configuration point in the previous chapter:
The controller @ResponseBody that no view points to is critical code that returns content directly without turning to the JSP view
@Controller public
class Mappingcontroller {
@RequestMapping ("/mapping/path") is public
@ResponseBody String Bypath () {return
' mapped by path! ';
}
}
Controller pointing to a specific view, no @responsebody, indicating that a JSP view is pointing
@Controller
@RequestMapping ("/views/*") public
class Viewscontroller {
@RequestMapping (value= "HTML") method=requestmethod.get) Public
String prepare (model model) {
model.addattribute ("foo", "Bar");
Model.addattribute ("Fruit", "apple");
return "views/html";
}
Four usage based on view steering:
1. Make a steering view of yourself
http://127.0.0.1:8010/views/html---> Corresponding views/html.jsp
@RequestMapping (value= "HTML", method= requestmethod.get) Public
String prepare (model model) {
model.addattribute ("foo", "Bar");
Model.addattribute ("Fruit", "apple");
return "views/html";
}
2.spring point to the default view, Org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator the default view to the turn, see log information:
DEBUG Dispatcherservlet Unable to locate requesttoviewnametranslator with Name ' Viewnametranslator ': Using default [Org.s PRINGFRAMEWORK.WEB.SERVLET.VIEW.DEFAULTREQUESTTOVIEWNAMETRANSLATOR@55A58F]
http://127.0.0.1:8010/views/viewName---> Corresponding views/viewname.jsp
@RequestMapping (value= "/viewname", method=requestmethod.get) public
void Usingrequesttoviewnametranslator (model model) {
Model.addattribute ("foo", "Bar");
Model.addattribute ("Fruit", "apple");
}
3.URI @PathVariable Acquisition, note that the parameter names must be consistent. Such as:
http://127.0.0.1:8010/views/pathVariables/bar/apple---> Corresponding views/html.jsp
@RequestMapping (value=) Pathvariables/{foo}/{fruit} ", Method=requestmethod.get) public
string Pathvars (@PathVariable string foo, @ Pathvariable String Fruit) {
//No need to add @PathVariables ' foo ' and ' fruit ' to the model
//They would be merge D in the model before rendering return
"views/html";
}
4. Assign values to JavaBean using the @pathvariable of the URL
Http://127.0.0.1:8010/views/dataBinding/bar/apple---> Corresponding views/databinding.jsp
@RequestMapping (value=) Databinding/{foo}/{fruit} ", method=requestmethod.get) public
String dataBinding (@Valid JavaBean JavaBean, Model Model) {
//JavaBean "foo" and "fruit" properties populated from URI variables return
"views/databinding";
}
public class JavaBean {
@NotNull
private String foo;
@NotNull
private String fruit;
Note that there is a get SET method, space is limited, not in the article
Group: Java EE system Architecture 203431569, before the group, please mark the development of the software industry and work experience.
Size: 70.7 KB View Picture Attachments