First carry out a wave of renderings
1.Controller
package com.tz.controller; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Required; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.tz.domain.Course; @Controller public class RequestController { /** * @PathVariable("user"):获取路径中的值/handler/{user} * @RequestParam("user"):获取请求参数/hander/{user}?user=coco * * @param userName * @return */ @RequestMapping("/handle") public String handler(@RequestParam(value="user",required=false) String userName){ //userName = request.getParameter("user"); System.out.println(userName); return "success"; } @RequestMapping("/handle2") public String handler02(@RequestParam(value="user",required=false,defaultValue="你没有带参数") String userName, @RequestHeader(value="User-Agent",required=false) String user_Agent, @CookieValue("JSESSIONID") String js){ //userName = request.getParameter("user"); System.out.println(userName); System.out.println (user_agent); * response: server -> client (server garbled) System.out.println(js); return "success"; } / ** * SpringMVC parameters can be written in the common servlet API * the HttpServletRequest * the HttpServletResponse * Httpsession * * the InputStream: request.getInputStream (); * the OutputStream: response.getOutputStream (); * Reader: request.getReader (); * Writer: response.getWriter (); * * request: client -> service (client garbled) * modify profile tomcat * the GET effectively * <Connector connectionTimeout = "20000" port = "8080" protocol = "HTTP /1.1 "the redirectPort =" 8443 "/> * request prior to obtaining the first parameter: Request.setCharacterEncoding (" UTF-. 8 "); * * reponse.setContentType (" text / HTML; charset = UTF-. 8 "); * @param c * @return */ @RequestMapping ( "/ the addCourse") public String the addCourse (Course, C) {// we will automatically springMVC defined type assignment / ** * 1. each object attribute attempt taken out from the request parameter, and packaged * request.getParameter ( "parameter name"); if the value, the package will be taken out to the properties of the entity class (call set methods) * / System.out.println (C); return "Success"; } @RequestMapping ( "/ handle04") public String handle04 (the HttpSession the session, the HttpServletRequest Request) { session.setAttribute ( "session", "session domain"); request.setAttribute ( "Request", "domain Request"); return "Success"; } }
2. configuration file web.xml (the most important thing is to set the character encoding filter)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>springmvc_Day01</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc</servlet-name> <the servlet-class> org.springframework.web.servlet.DispatcherServlet </ the servlet-class> <-! <the init-param> param-name> <the contextConfigLocation </ param-name> Specifies the location springmvc profile <param- value> the CLASSPATH: springmvc.xml </ param-value> </ the init-param> -> ! <- If you do not specify the file location in web.xml, will find the default file / WEB-INF / xxx-servlet .xml -> <-! bootloader period -> <-! the servlet is loaded when the server starts to create an object, the smaller the value, the higher the priority on behalf of, the more to create objects -> <! - <the init-param> <param-name> the contextConfigLocation </ param-name> <param-value> CLASSPATH: springmvc.xml </ param-value> </ the init-param> -> ! <- if web.xml file location is not found, the default will be to find the file /WEB-INF/xxx-servlet.xml -> <Load-ON-Startup>. 1 </ Load-ON-Startup> </ the servlet> <the servlet-Mapping> <the servlet-name> SpringMVC </ the servlet-name> <URL-pattern> / </ URL-pattern> < ! - / and / * are used to intercept all requests, but a wider range of / * and * .jsp request will be intercepted, once intercepted, access jsp page will not show / will intercept all requests , but will not block .jsp, can guarantee the normal jsp page views * .do .action * * .hah /: * .jsp does not intercept jsp / *: interceptor jsp -> </ Mapping-the servlet> ! <- configuration character encoding filter after the filter prior to ensure all the filters, to avoid intercepting the request can not be so in the first -> <filter> <filter-name> CharacterEncodingFilter </ filter-name> <filter-class> ORG .springframework.web.filter.CharacterEncodingFilter </ filter-class> <- encoding:! specified resolution request distortion forceRequestEncoding:指定请求与响应的编码集格式为eccoding forceResponseEncoding --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <-! Reset style requests get queries put a request to modify delect delete post New -> <filter> <filter-name> HiddenHttpMethodFilter </ filter-name> <filter-class> org.springframework.web.filter.HiddenHttpMethodFilter </ filter-class> </ filter> <filter-Mapping> <filter-name> HiddenHttpMethodFilter </ filter-name> <-! .jsp here will be filtered out -> <URL-pattern> / * </ pattern-URL> </ filter-Mapping> </ Web-App>