Springmvctest.java
Package com.wxh.springmvc.handlers;
Import java.io.IOException;
Import Java.io.Writer;
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 Com.wxh.springmvc.entities.User;
@RequestMapping ("/springmvc") @Controller public class Springmvctest {private static final String SUCCESS = "SUCCESS";
/** * Serlvet Native APIs can be used as parameters of the target method to specifically support the following types * * httpservletrequest * httpservletresponse * HttpSession * Java.security.Principal * Locale InputStream
* OutputStream * Reader * Writer * @throws IOException/@RequestMapping ("/testservletapi") public void TESTSERVLETAPI (HttpServletRequest request, httpservletresponse response, Writer out) throws ioexception{
Rintln ("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.P
Rintln ("Testpojo:" + user);
return SUCCESS; /** * Understanding: * @CookieValue: Map A cookie value, properties with @requestparam * * * @RequestMapping ("/testcookievalue") public St Ring Testcookievalue (@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 405 problem @RequestMapPing (value= "/testrest/{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" >