Article directory
I. Basic operation
How to respond to requests
Serial number | Response mode | Explain |
---|---|---|
1 | No response | void+@ResponseBody annotation |
2 | ModelAndView | Through the setViewName method |
3 | Specify the response page directly | The return value is of String type, and the return result specifies the jump address |
4 | redirect | Add the prefix redirect before the jump address |
5 | HttpServletRequest and HttpServletResponse | These two variables are declared in the parameter. Then jump through the relevant api |
1. ModelAndView mode
/** *Annotation based custom Controller * @author Administrator * */ @Controller @RequestMapping("/hello") // http: /.. / project name / hello public class SpringController { /** * Server response page * ModelAndView * @return */ @RequestMapping("/h1") public ModelAndView hello1(){ System.out.println("---Spring1----"); ModelAndView m=new ModelAndView(); // Add '/' to indicate absolute road strength and not to indicate relative path m.setViewName("/index.jsp"); return m; } }
2. The server does not respond to the result to the client
/** * The server does not respond to the result to the client * void+@ResponseBody * @return */ @RequestMapping("/f1") @ResponseBody public void fun1(){ System.out.println("---f1----"); }
Returns a string
/** * Server response string to client * * @return */ @RequestMapping("/f2") @ResponseBody public String fun2(){ System.out.println("---f2----"); return "String"; }
4. Return to a page
Absolute path
/** * Server responds to a page absolute path * * @return */ @RequestMapping("/f3") public String fun3(){ System.out.println("---f3----"); return "/index.jsp"; }
Relative path
/** * Server responds to a page relative path * * @return */ @RequestMapping("/f4") public String fun4(){ System.out.println("---f4----"); return "index.jsp"; }
404 the error reporting relative path is the index.jsp file in the hello folder
5. Prefix and suffix
Member variables in the view parser
Configure view resolver
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- Open scan --> <context:component-scan base-package="com.sxt.controller"/> <!-- open SpringMVC How to annotate --> <mvc:annotation-driven></mvc:annotation-driven> <!-- Configure view resolver address --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Suffix before configuration --> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
Redirect (add a redirect)
@RequestMapping("/f5") public String fun5(){ System.out.println("---f5----"); //The default is server-side adjustment. If you need to redirect, add the prefix redirect: return "redirect:/index.jsp"; }
HttpServletRequest and HttpServletResponse
/** * Traditional way * @param req * @param response * @throws ServletException * @throws IOException */ @RequestMapping("/f6") public void fun6(HttpServletRequest req,HttpServletResponse response) throws ServletException, IOException{ System.out.println("-----f6--------"); req.getRequestDispatcher("/index.jsp").forward(req, response); }