Response processing of spring MVC

1. Transfer request data to background processing

1.1 use the default built-in view parser · ViewResolver

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
</bean>

1.2 using view controller · ViewController

Jump directly to the page without background logical processing

<mvc:view-controller path="/" view-name="index" />

be careful! There must be a file with the same name under the web > WEB-INF folder, otherwise an error will be reported

2. Transfer data to view

2.1 use Model, Map and ModelMap to transfer data to the page

  • Model mode
@RequestMapping("/testmodel")
public String testModel(Model model){
	model.setAttribute("key01","value");
    return "index";
}
  • Map mode
@RequestMapping("/testmap")
public String testMap(Map map){
	map.setAttribute("key01","value");
    return "index";
}
  • ModelMap mode
@RequestMapping("/testmodelmap")
public String testModelMap(ModelMap modelmap){
	modelmap.put("key01","value");
    return "index";
}

The bottom layers of these three methods are transmitted, and their common implementation class is BindingAwareModelMap

2.2 using ModelAndView object to transfer data to page

ModelAndView can encapsulate both data and views

@RequestMapping("/testmodelandview")
public String testModelAndValue() {
    ModelAndView modelandvalue = new ModelAndView("main");
	modelandvalue.setAttribute("key01","value");
    return modelandvalue;
}

2.3 using session to transfer data to the page

How to set the session property

1. Read and write session through servlet api

  • Get servlet api by parameter binding
@RequestMapping("/testsession01")
public String testSession01(HttpSession session){
	session.setAttribute("key01","value");
    return "index";
}
  • Get servlet api through automatic injection
@Autowried
private HttpSession session;
@RequestMapping("/testsession02")
public String testSession02(){
	session.setAttribute("key02","value");
    return "index";
}

2. Read and write session s through annotations provided by spring MVC

  • @SessionAttributes
    • It is used on the class and is responsible for writing the session, which is effective for all methods under the current class
    • Depending on the model, the bottom layer is to find the attribute with the same name from the model. If it is found, set the same in the session
  • @SessionAttribute
    • It is used on parameters and is responsible for reading session
    • Model and session can obtain and write data from each other. Setting @ SessionAttribute(value="mytest") in front of the parameter is equivalent to implicitly writing this parameter to the model
  • @SessionAttribute(value="mytest", required=false) can be used for setting. This attribute must not exist (otherwise, a 400 error will be reported when it is not passed in)
@RequestMapping("/testsessionattribute")
public String testSessionAttribute(@SessionAttribute("mytest") String mytest){
	System.out.println(mytest);
    return "index";
}

4. Use @ ModelAttribute to get the data in the request

Common scenarios

/ / the @ModelAttribute modification method will call @Autowried private HttpSession session before all methods in the current processor; @ ModelAttribute public void testModelAttribute(HttpSession s){ this.session=s; }

The method of @ModelAttribute modification will be called before all the methods in the current processor.

  • Global variables can be assigned
@Autowried
private HttpSession session;
@ModelAttribute
public void testModelAttribute(HttpSession s){
	this.session=s; 
}
  • When updating the database, if there are only a few modified fields provided by the user, some field updates will be lost
    • Before parameter binding, spring MVC will take out and merge the attributes in the model that match the parameter name. The newly submitted fields will be overwritten and the missing ones will be retained (for some frameworks that cannot customize SQL statements, such as Hibernate)

On parameters

Can be omitted

Three ways to obtain thread safety in Servlet api

Thread unsafe = concurrency problem

At the same time, multiple threads read and write shared data / variables / resources at the same time, which will lead to concurrency problems

1. By parameter binding

Thread safety, because the method variable of parameter binding is method level, and each request method will open up its own independent space in memory

2. Automatic injection through @ Autowired

Thread safe, special (it looks like a shared servlet, but the spring MVC underlying servlet is stored through ThreadLocal)

3. Through @ ModelAtrribute

Non thread safe because it is a shared variable

// The method of @ModelAttribute modification will be called before all the methods in the current processor.
@Autowried
private HttpSession session;
@ModelAttribute
public void testModelAttribute(HttpSession s){
	this.session=s; 
}

5. Use forward to realize page forwarding

characteristic:

  • The request in the address bar will not change, and the first requested address is displayed
  • Number of requests: 1
  • Data for the requested domain will not be lost
  • The root directory localhost:8080 / project address /, which contains the access address of the project

6. Redirect using redirect

characteristic:

  • The address in the address bar changes to display the latest requested address
  • Number of requests: 2
  • Data in the request domain will be lost because it is a different request
  • Root directory: localhost:8080 / does not contain the name of the project (the project name will be included in spring MVC regardless of forwarding or redirection)

Keywords: Java Spring MVC Annotation

Added by Boo-urns on Mon, 27 Dec 2021 10:44:00 +0200