Several ways in which page values are passed back to the background in Spring MVC

Source: Internet
Author: User

In the process of learning Spring MVC, it is necessary to understand several key parameters first:
@Controller:
Annotations on classes, this class will program a controller, and Spring will automatically scan this class when the project starts, and make corresponding URL route mappings.
@Controller
public class Useraction
{

}
@RequestMapping
Specifies the URL mapping path, if the requestmapping is configured on the controller, the specific request method also configures the path to map the path to the overlay common mappings for both paths such as: Requestmapping ("url.html")
To configure the mapping path:

@Controller
public class Useraction
{
@RequestMapping (value = "/get_alluser.html")
Public Modelandview Getalluser (String Id)
{
}
}

Above configuration mappings
Http://***:8080:web1/get_alluser.html:
If you add @RequestMapping (value = "/user") in the @Controller, the mapping path becomes
Http://***:8080:web1/user/get_alluser.html
@ResponseBody
Returns the string corresponding to the annotation method directly
@RequestParam
Automatically map the URL corresponding to the value above the action, requestparam default is required parameter.

@PathVariable
Gets the URL mapping parameters for the specified format of the @requestmapping configuration

/*
* Direct output HTML, or JSON string
* Request Path:
*/WEB1/URLINFO/GETCONTENT.HTML?KEY=RHYTHMK
*/WEB1/URLINFO/GETCONTENT.JSON?KEY=RHYTHMK
* */
@ResponseBody
@RequestMapping (value = "/getcontent.**")
Public String getcontent (
@RequestParam ("key") String key,
@RequestParam (value = "Key2", required = false, DefaultValue = "defaultvalue") String Key2) {
System.out.println ("GetContent is called");
String result = "Return content directly-key:" + key + ", Key2:" + key2;
SYSTEM.OUT.PRINTLN (result);
return result;
}

/*
* Requestmapping supports ANT-style URL configuration:
* Request Path:
*/URLINFO/GETURLANT/CONFIG.HTML?KEY=ADDDD
*/
@ResponseBody
@RequestMapping (value = "/geturlant/**.html")
Public String geturlant (HttpServletRequest request) {
String result = "?" After the argument is: "+ request.getquerystring ();
return result;
}

/*
* Configure URLs in the specified format to map to corresponding parameters
* Request Path:/web1/urlinfo/geturlparam/12_123.html
*
* */

@RequestMapping (value = "/geturlparam/{id}_{menuid}.html")
Public Modelandview Geturlparam (@PathVariable ("id") String ID,
@PathVariable ("MenuId") String menuId) {
Modelandview mode = new Modelandview (showmsg);
Mode.addobject ("msg", "Get to the ID:" + ID + ", menuId:" + menuId);
return mode;
}

/*
* Receive POST requests only
*/
@ResponseBody
@RequestMapping (value = "/posturl.html", method = Requestmethod.post)
public string Urlmethod (@RequestParam string id) {
Return "can only be a POST request, get to the ID:" + ID;
}

/*
* Write Cookie
* */
@RequestMapping ("/writecookies.html")
Public Modelandview writecookies (@RequestParam String value,
HttpServletResponse response) {

Response.addcookie (New Cookie ("key", value));
Modelandview mode = new Modelandview (showmsg);
Mode.addobject ("msg", "Cookies written successfully");
return mode;
}

/*
* The value of the corresponding key is obtained by @CookieValue
* */
@RequestMapping ("/getcookies.html")
Public Modelandview GetCookie (@CookieValue ("key") String Cookvalue) {
Modelandview mode = new Modelandview (showmsg);
Mode.addobject ("msg", "cookies=" + cookvalue);
return mode;
}

/*
* Pass the Servlet Api as a parameter
* Httpservletresponse,httpservletrequest can be used directly in action
* */
@RequestMapping ("/servlet.html")
Public String Servlet1 (httpservletresponse response,
HttpServletRequest request) {

Boolean result = (Request! = NULL && response! = NULL);
Modelandview mode = new Modelandview ();
Mode.addobject ("msg", "result=" + result.tostring ());
return showmsg;

}

/*
* Instantiate an object based on the parameters passed in by the URL
*
* such as: Http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad
* */
@RequestMapping ("getobject.html")
Public Modelandview getObject (UserInfo user) {
String result = "User id:" + user.getuserid (). toString () + ", User name:"
+ User.getusername (). toString ();
Modelandview mode = new Modelandview (showmsg);
Mode.addobject ("msg", "result=" + result.tostring ());
return mode;
}


To implement a page jump:

/*
* Implement page Jump
*/web1/urlinfo/redirectpage.html
* */
@RequestMapping ("/redirectpage.html")
Public String Redirectpage ()
{
return "redirect:getcookies.html?r=10";

}


Direct Callback JSON
The requested URL must end with. JSON, otherwise the exception
Failed to load Resource:the Server responded with a status of 406 (not acceptable): the resource identified by this requ EST is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()
Callback Entity:

@JsonSerialize (include = JsonSerialize.Inclusion.NON_NULL)
public class UserInfo {

Private Integer UserId;
Public Integer getUserId () {
return UserId;
}
public void Setuserid (Integer userId) {
UserID = userid;
}
Public String GetUserName () {
return UserName;
}
public void Setusername (String userName) {
UserName = UserName;
}
Private String UserName;


}

Callback Action

@ResponseBody
@RequestMapping ("/getuser.json")
Public UserInfo GetUser ()
{
System.out.println ("GetUser");
UserInfo model=new UserInfo ();
Model.setuserid (100);
Model.setusername ("Wang Kun");
return model;
}

Request:
/web1/urlinfo/getuser.json
Output:
{"UserId": +, "userName": "Wang Kun"}

Several ways in which page values are passed back to the background in Spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.