The SESSIONATTRIBUTES_SPRINGMVC of SPRINGMVC_ processing model data

Source: Internet
Author: User
Tags class definition

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.bind.annotation.SessionAttributes;

Import Org.springframework.web.servlet.ModelAndView;

Import Com.wxh.springmvc.entities.User; @SessionAttributes (value={"user"},types={string.class}) @RequestMapping ("/springmvc") @Controller public CLass Springmvctest {private static final String SUCCESS = "SUCCESS"; 
	 /** * @SessionAttributes You can specify which model properties are to be placed in the session (actually using the types property value) through the object type of the Model property, in addition to specifying the attributes that need to be placed in the session (actually using the Value property) through the property name. * Note: This annotation can only be placed on top of the class.
	 Instead of modifying the method.  */@RequestMapping ("/testsessionattributes") public String testsessionattributes (map<string,object> Map) {User
		user = new User ("Tom", "1111", "1@11.com", "12");
		Map.put ("user", user);
		Map.put ("School", "Qinghua");
	return SUCCESS; /** * Target method can add a map type (actually can also be a Model type or MODELMAP type) parameter * @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 the Modelandview type.
	 * It can contain view and model information * SPRINGMVC will put data from 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; /** * 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.P		
		Rintln ("Testpojo:" + user);
	return SUCCESS; /** * Understanding: * @CookieValue: Map a CoOkie value, the property is the same as @requestparam/@RequestMapping ("/testcookievalue") public String Testcookievalue (The @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; /** * Rest-style URL * For crud example: * Add:/order POST * Modify:/ORDER/1 put update?id=1
	 * Obtain:/ORDER/1 get get?id=1 * Delete:/ORDER/1 Delete delete?id=1 * * How to send put requests and delete requests. * 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" >

User.java

Package com.wxh.springmvc.entities;
	public class User {private String username;
	
	private String password;
	Private String Email;
	
	Private String age;
	
	private address address;
	Public String GetUserName () {return username;
	} public void Setusername (String username) {this.username = username;
	Public String GetPassword () {return password;
	} public void SetPassword (String password) {this.password = password;
	Public String Getemail () {return email;
	public void Setemail (String email) {this.email = email;
	Public String Getage () {return age;
	public void Setage (String age) {this.age = age;
	Public Address getaddress () {return address;
	public void setaddress (address) {this.address = address;  @Override public String toString () {return "User [username=" + Username + ", password=" + password +, email= "+
	Email + ", age=" + Age + ", address=" + address + "]"; Public User (string username, string passworD, string email, string age) {super ();
		This.username = Username;
		This.password = password;
		This.email = email;
	This.age = age;
 Public User () {}}

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.