Springmvctest.java
Package com.wxh.springmvc.handlers;
Import java.io.IOException;
Import Java.io.Writer;
Import Java.util.Arrays;
Import Java.util.Date;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.bind.annotation.CookieValue;
Import org.springframework.web.bind.annotation.PathVariable;
Import Org.springframework.web.bind.annotation.RequestHeader;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Org.springframework.web.servlet.ModelAndView;
Import Com.wxh.springmvc.entities.User;
@RequestMapping ("/springmvc") @Controller public class Springmvctest {private static final String SUCCESS = "SUCCESS"; /** * Target method you can add a map type (it can actually be a Model type or ModelmaP-Type) parameters * @param map * @return */@RequestMapping ("/testmap") public String TestMap (map<string,object> map)
{System.out.println (Map.getclass (). GetName ());
Map.put ("Names", Arrays.aslist ("Tom", "Jerry", "Mike"));
return SUCCESS;
}/** * The return value of the target method can be a modelandview type.
* This can include view and model information * SPRINGMVC will put the data in the Modelandview model into the request domain object.
* @return */@RequestMapping ("/testmodelandview") public Modelandview Testmodelandview () {String viewName = SUCCESS;
Modelandview Modelandview = new Modelandview (viewName);
Add model data to Modelandview.
Modelandview.addobject ("Time", New Date ());
return modelandview; /** * can use the Serlvet native API as the target method parameter specifically supports the following types of * * HttpServletRequest * httpservletresponse * Httpsessi
On * Java.security.Principal * Locale inputstream * outputstream * Reader * Writer * @throws IOException */@RequestMapping ("/testservletapi") public void Testservletapi (HttpServletRequest request, HttpserVletresponse response, Writer out) throws ioexception{System.out.println ("Testservletapi," + Request + "," + response
);
Out.write ("Hello Springmvc");
return SUCCESS;
}/** * Spring MVC automatically matches the request parameter name and the Pojo property name * Automatically populates the property values for the object, supporting cascading properties. * such as: Dept.deptid, Dept.address.tel, etc. */@RequestMapping ("/testpojo") public String Testpojo (user user) {SYSTEM.OUT.P
Rintln ("Testpojo:" + user);
return SUCCESS; }/** * Learn: * @CookieValue: Maps a cookie value, property with @requestparam */@RequestMapping ("/testcookievalue") public String t Estcookievalue (@CookieValue ("Jsessionid") String sessionId) {System.out.println ("Testcookievalue:sessionid:" +
SESSIONID);
return SUCCESS; }/** * Understand: * Map Request header information * Usage same as @requestparam * @return */@RequestMapping ("/testrequestheader") public Strin G Testrequestheader (@RequestHeader (value= "Accept-language") String al) {System.out.println ("Testrequestheader,
Accept-language: "+ al);
return SUCCESS; }/** * * @RequestParaM to map the request parameter * value value is the parameter name of the request parameter * required The parameter must be true * DefaultValue the default value of the request parameter */@RequestMapping ("/testrequest Param ") public string Testrequestparam (@RequestParam (value=" username ") of string un, @RequestParam (value=" age ", required
=false,defaultvalue= "0") int age) {System.out.println ("Testrequestparam,username" +un+ ", Age:" +age);
return SUCCESS; }/** * Rest-style URL * Take crud for example: * Add:/order POST * Modify:/ORDER/1 PUT update?id=1 * Get:/ORDER/1 get
get?id=1 * Delete:/ORDER/1 Delete delete?id=1 * * How to send a PUT request and delete request. * 1. Need to configure Hiddenhttpmethodfilter * 2. Need to send a POST request * 3. Need to carry a name= "_method" de hidden field when sending a POST request, with a value of delete or PUT * * in SPRI
How do I get the ID in the target method in Ngmvc? * Use @pathvariable Annotations * * @param ID * @return */@ResponseBody ()//Add this annotation to resolve 405 problem @RequestMapping (value= "/test Rest/{id} ", method=requestmethod.put) public String testrestput (@PathVariable Integer id) {SYSTEM.OUT.PRINTLN ("
Testrest PUT: "+id);
return SUCCESS; } @RequEstmapping (value= "/testrest/{id}", Method=requestmethod.delete) @ResponseBody ()//Add this annotation to resolve the issue of 405 public String
Testrestdelete (@PathVariable Integer id) {System.out.println ("Testrest Delete:" +id);
return SUCCESS; } @RequestMapping (value= "/testrest", method=requestmethod.post) public String testrest () {System.out.println ("Testre
St POST ");
return SUCCESS;
} @RequestMapping (value= "/testrest/{id}", method=requestmethod.get) public String testrest (@PathVariable Integer ID) {
System.out.println ("Testrest GET:" +id);
return SUCCESS;
}/** * @PathVariable can map placeholders in URLs to the parameters of the target method. * @param ID * @return */@RequestMapping ("/testpathvariable/{id}") Public String testpathvariable (@PathVariable ("id"
Integer ID) {System.out.println ("testpathvariable:" + ID);
return SUCCESS;
} @RequestMapping ("/TESTANTPATH/*/ABC") public String Testandpath () {System.out.println ("Testantpath");
return SUCCESS; }/** * Learn: You can use params and headers to map requests more precisely, params and HeadeRS supports simple expressions * @return */@RequestMapping (value= "Testparamsandheaders", params={"username", "age!=10"}, headers={"Ac
cept-language=en-us,zh;q=0.8 "}) public String testparamsandheaders () {System.out.println (" testparamsandheaders ");
return SUCCESS;
}/** * Common: Use the Method property to specify the request method * @return */@RequestMapping (value= "/testmethod", Method=requestmethod.post)
Public String TestMethod () {System.err.println ("TestMethod");
return SUCCESS;
}/** * 1. @RequestMapping In addition to the modification method, you can also modify class * 2.
* 1). Class definition: Provides preliminary request mapping information relative to the Web app's root directory * 2). Method: Provides further subdivision mapping information. * Relative to the URL at the class definition. If @requestmapping is not marked at the class definition.
The URL that is tagged at the method is relative to the Web app's root directory. * @return */@RequestMapping ("/testrequestmapping") public String testrequestmapping () {System.out.println ("Testre
Questmapping ");
return SUCCESS;
}
}
Index.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
success.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >