This article is based on SPRINGMVC annotations to implement the parameters between the page and the background of the transfer, this article mainly focus on from the background to the foreground view. Content: 1. Pass a single object in the background user to the foreground; 2, the background passes the collection object to the foreground (in the case of map and list two) the following content is based on the SPRINGMVC project configuration is ready, the project was built with Maven. One: Pass a single object (the user object, for example) the users class:
public class User { private Integer userId; User name private String userName; Password private String password; Public Integer getUserId () { return userId; } public void Setuserid (Integer userid) {this . userid = userid; } 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; }}
Controller class:
@Controller @requestmapping (value= "/test") public class Democontroller { @RequestMapping (value= "/param") Public String Testparam (model model) { User u1 = new User (); U1.setuserid (1); U1.setpassword ("123456"); U1.setusername ("Zhang San"); Model.addattribute ("User", U1); return "param"; }}
param.jsp page:
<%@ page language = "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%> <% @taglib prefix =" c "uri=" Http://java.sun.com/jsp/jstl/core " %>
Second: Pass the Map Collection object (take map<string,user> as an example)user-similar on;Controller class:@Controller @requestmapping (value= "/test") public class Democontroller {@RequestMapping (value= '/param ') public St Ring Testparam (model model) {return mapInfo (); }/* * Modelandview: It represents the model and view used in spring WEB MVC to render the screen, * since Java can only return one object at a time, the role of Modelandview encapsulates the two objects to facilitate your Returns both the model and view objects at once. * @return */private Modelandview MapInfo () {map<string,object> Map = new hashmap<string, Obje Ct> (); User U1 = new user (); U1.setuserid (1); U1.setpassword ("123456"); U1.setusername ("Zhang San"); User U2 = new user (); U2.setuserid (1); U2.setpassword ("123456"); U2.setusername ("Zhang Si"); Map.put ("user1", U1); Map.put ("User2", U2); /* * 1, "param": Try the name, that is, jump to param.jsp page * 2, "map": the object to be passed map type * 3, Map: The object being passed */ return new Modelandview ("pAram "," map ", map); }}
param.jsp page:<%@ page language = "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%> <% @taglib prefix =" c "uri=" Http://java.sun.com/jsp/jstl/core " %>
Three: Passing the List collection object (take list<user> as an example)user-similar on;Controller class: @Controller @requestmap Ping (value= "/test") public class Democontroller {@RequestMapping (value= "/param") public String Testparam (Model Model) {Model.addattribute (Listinfo ()); return "Param"; Public @ModelAttribute list<user> Listinfo () {list<user> List = new arraylist<user> (); User U1 = new user (); U1.setuserid (1); U1.setpassword ("123456"); U1.setusername ("Zhang San"); User U2 = new user (); U2.setuserid (1); U2.setpassword ("123456"); U2.setusername ("Zhang Si"); User U3 = new user (); U3.setuserid (1); U3.setpassword ("123456"); U3.setusername ("Zhang Five"); List.add (U3); List.add (U2); List.add (U1); return list; }}
param.jsp page:<%@ page language = "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%> <% @taglib prefix =" c "uri=" Http://java.sun.com/jsp/jstl/core " %>
Springmvc parameter Passing method List,map