Springmvc get Parameters
1. guide package, four core packages, one cut-bread (AOP), logging, web, springmvc
2. The core code of the configuration file is as follows:
Web. xml
<Servlet> <servlet-name> springDispatcherServlet </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: springmvc. xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <! -- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name> springDispatcherServlet </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <! -- The character encoding filter must be placed at the beginning --> <filter-name> CharacterEncodingFilter </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <! -- Configure encoding, tells us the specified encoding format --> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init -param> <! -- Fix response garbled characters --> <init-param> <param-name> forceEncoding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> CharacterEncodingFilter </filter-name> <url-pattern>/</url-pattern> </filter-mapping> <! -- Supports rest filters --> <filter-name> HiddenHttpMethodFilter </filter-name> <filter-class> org. springframework. web. filter. hiddenHttpMethodFilter </filter-class> </filter> <filter-mapping> <filter-name> HiddenHttpMethodFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
Springmvc. xml
<Context: component-scan base-package = "com. atguigu "> </context: component-scan> <bean class =" org. springframework. web. servlet. view. internalResourceViewResolver "> <! -- View analyzer --> <property name = "prefix" value = "/WEB-INF/pages/"> </property> <property name = "suffix" value = ". jsp "> </property> </bean> </beans>
Index. jsp: the home page is displayed.
<Body> <a href = "hello"> hello </a> <br/> <a href = "handle01? User = 123456 "> GET Request Parameters </a> <br/> <a href =" handle02 "> GET request header </a> <br/> <form action =" saveBook "method =" post "> book id <input type =" text "name =" id "/> <br/> book name <input type =" text "name =" name "/> <br/> book author <input type =" text "name =" author "/> <br/> book price <input type =" text "name =" price "/> <br/> book sales <input type =" text "name =" sales "/> <br/> book stock <input type =" text "name =" stock "/> <br/>
3./WEB-INF/pages content after jump
1). success. jsp
<Body>
2). testScope. jsp
<Body>
4. src/bean package Author. java
public class Author { private String name; private String address;
Book. java:
public class Book { private int id; private String name; private double price; private int sales; private int stock; private Author person; private String imgPath = "static/img/default.jpg"; private String author;
Src/controller package, HelloController. java: cannot be accessed if not added
Import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; @ Controllerpublic class HelloController {@ RequestMapping ("/hello") // The Connection address must contain "/hello" public String hello () {return "success ";}}
TestParamController. java
@ Controllerpublic class TestParamController {/*** 1. directly write a variable with the same name as the request parameter to the parameter location of the Method * 2. This variable is encapsulated to bring * user = request. getParameter ("user") * 3. If not, it is null ** @ RequestParam ("user"): specify the value of which parameter to obtain * 1. This parameter must be included in the default request; * otherwise: HTTP Status 400-Required String parameter 'user' is not present * 2. required = false can be set as not Required; no null * 3, defaultValue = "not named" specifies the default value when no; * user = request. getParameter ("user"); */@ RequestMapping ("/handle01") public String handle01 (@ RequestParam (value = "user", required = false, defaultValue = "") string user) {System. out. println ("obtained User:" + user); return "success" ;}/ *** GET request header; * request. the getHeader ("User-Agent") * annotation does the following: * @ RequestHeader ("User-Agent") String userAgent; * userAgent = request. getHeader ("User-Agent"); */@ RequestMapping ("/handle02") public String handle02 (@ RequestHeader (value = "User-Agent", required = false, defaultValue = "NONE") String userAgent) {System. out. println ("User-Agent:" + userAgent); return "success";}/*** get the value of a cookie; * JSESSIONID = required * @ CookieValue ("JSESSIONID ") the String jid * annotation does the following: * Cookie [] cookies = request. getCookies (); * for (Cookie c: cookies) {* if (c. getName (). equals ("JSESSIONID") {* jid = c. getValue (); *} */@ RequestMapping ("/handle03") public String handle03 (@ CookieValue (value = "JSESSIONID", required = false, defaultValue = "hhhhh") String jid) {System. out. println ("jid:" + jid); return "success";}/* Incoming POJO; directly encapsulates the page values. This is easy to use and requires less code to implement code separation, decoupling and * 1. book = new Book (); * 2. Check the value of each attribute in the book object and request. getParameter (attribute); * 3. Set this value. * Note: Because SpringMVC performs type conversion, the submitted data must be valid, otherwise, error 400 */@ RequestMapping ("/saveBook") public String handle04 (Book book) {System. out. println ("book value:" + book); return "success" ;}@ RequestMapping ("/handle05 ") // pringMVC also allows us to use the native ServletAPI HttpServletRequest HttpServletResponse // HttpSession public String handle05 (HttpSession session, HttpServletRequest request, HttpServletResponse response) {session. setAttribute ("msg", "HAHAHA"); request. setAttribute ("reqMsg", ""); return "success ";}}
Src/dataout/DataOutPutController. java carries data to the page
@ Controller // bring the public class DataOutPutController {/*** to the page. 1. Change the returned value to ModelAndView (including Model data and View )); * data is stored in the request domain. * 2. Data can be uploaded to Model, Map, or ModelMap in the request parameters; the data stored in them will be placed in the request domain * Model, Map, and ModelMap. In the end, BindingAwareModelMap will be useful again; * saving data to BindingAwareModelMap is to save * Model Map * to the request domain. | * | \/* | ModelMap * \/* ExtendedModelMap [implicit Model] extends ModelMap implements Model * \/* BindingAwareModelMap * @ return */@ RequestMapping ("/output04 ") public String output04 (ModelMap model) {// The View parser Concatenates the image name into a model. addAttribute ("msg", "output04"); System. out. println (model. getClass (); return "testScope" ;}@ RequestMapping ("/output03") public String output03 (Model model) {model. addAttribute ("msg", "output03"); System. out. println (model. getClass (); return "testScope" ;}@ RequestMapping ("/output02") public String output02 (Map <String, Object> map) {// The View parser Concatenates the map with the view name. put ("msg", "output02"); System. out. println (map. getClass (); return "testScope" ;}@ RequestMapping ("/output01") public ModelAndView output01 () {// The View parser concatenates ModelAndView modelAndView = new ModelAndView (); modelAndView. setViewName ("testScope"); modelAndView. addObject ("msg", "output01"); return modelAndView ;}}