Springmvctest.java
Package com.wxh.springmvc.handlers;
Import java.io.IOException;
Import Java.io.Writer;
Import Java.util.Date;
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";
/** * The return value of the target method can be a modelandview type. * which can contain view and model information * SPRINGMVC will put the data in the Modelandview model intoTo 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; /** * You can use the Serlvet native API as a target method parameter to specifically support the following types * * httpservletrequest * httpservletresponse * Httpsessi
On * Java.security.Principal * Locale inputstream * outputstream * Reader * Writer * @throws IOException * * @RequestMapping ("/testservletapi") public void Testservletapi (HttpServletRequest request, HttpServletResponse R
Esponse, 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 to automatically populate the object with attribute values, supporting cascading properties. * such as: Dept.deptid, Dept.address.tel and other * * * @RequestMapping ("/testpojo") public String Testpojo (useR user) {System.out.println ("Testpojo:" + user);
return SUCCESS; /** * Understanding: * @CookieValue: Map A cookie value, properties with @requestparam * * * @RequestMapping ("/testcookievalue") public String t Estcookievalue (@CookieValue ("Jsessionid") String sessionId) {System.out.println ("Testcookievalue:sessionid:" +
SESSIONID);
return SUCCESS; /** * Understanding: * Mapping Request header information * usage with @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 whether the argument must be, default to True * DefaultValue the default value of the request parameter * * @RequestMapping ("/testrequestparam") public string Testrequestparam (@RequestParam (value= "username") string un, @Requ Estparam (value= "age", required=false,defaultvalue= "0") int age) {System.out.println ("Testrequestparam,username" +un
+ ", Age:" +age);return SUCCESS; /** * RESTful URL * Take crud as an example: * Add:/order POST * Modify:/ORDER/1 put update?id=1 * Fetch:/ORDER/1 get
get?id=1 * Delete:/ORDER/1 Delete delete?id=1 * * How to send a put request and a delete request. * 1. Need to configure Hiddenhttpmethodfilter * 2. Send a POST request * 3. You need to carry a POST request with a name= "_method" de hidden field, the value is delete or put * * in SPRI
How do I get an ID in the target method in Ngmvc? * Use @pathvariable annotation * * * @param ID * @return/@ResponseBody ()//Add this annotation to solve the problem of 405 @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 ()//plus this annotation resolves 405 of the problem 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;
The/** * @PathVariable can be used to map placeholders in URLs to parameters in 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; /** * Understanding: You can use params and headers for more precise mapping requests, params and headers support simple expressions * @return/@RequestMapping (value= "Testparam") Sandheaders ", params={" username "," age!=10 "}, headers={" accept-language=en-us,zh;q=0.8 "}) public String
Testparamsandheaders () {System.out.println ("testparamsandheaders");
return SUCCESS; /** * Common use: Specify the request by using the Method property * @return/@RequestMapping (value= "/testmethOd ", method=requestmethod.post) public String TestMethod () {System.err.println (" TestMethod ");
return SUCCESS;
/** * 1. @RequestMapping In addition to cosmetic methods, you can also modify the class * 2.
* 1). Class definition: Provides preliminary request mapping information relative to Web application root * 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 the method marks is relative to the root directory of the Web application. * @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" >