欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring mvc参数传递

程序员文章站 2022-04-17 20:13:48
...
1.使用HttpServletRequest request
2.使用字符串
public class UserController {
	@RequestMapping(value="/user/list.do")
	public String list(HttpServletRequest request,String passWord){
		String userName=request.getParameter("userName");
		System.out.println("listtttttttttttt");
		System.out.println("userName:"+userName);
		System.out.println("password:"+passWord);
		return "users/user-list";
	}


首页index.jsp
<form action="user/list.do" method="post">  
    userName:<input type="text" name="userName" />  
    passWord:<input type="text" name="passWord" />  
    <input type="submit" value="tijiao" />  
</form>  

3.使用默认参数
@Controller
//@RequestMapping("/user")
public class UserController {
	@RequestMapping(value="/user/list.do")
	public String list(@RequestParam(value="user",defaultValue="haoning")String userName,String passWord){
		
		System.out.println("userName:"+userName);
		System.out.println("password:"+passWord);
		return "users/user-list";
	}

}
//默认的userName为haoning,要给userName起个别名


4.传递对象参数
建一个存用户信息的类UserInfo.java
package zou.huiying;

public class UserInfo {
	private String userId;
	private String userName;
	private String userSex;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserSex() {
		return userSex;
	}
	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}
}


配置控制器
@RequestMapping(value="/user/list.do")
	public String add1(UserInfo user){
		System.out.println("user:"+user);
		System.out.println("userId:"+user.getUserId());
                System.out.println("userName:"+user.getUserName());
		System.out.println("userSex:"+user.getUserSex());
		return "users/user-list";
	}
//user是UserInfo的一个实例


传递信息的页面add1.jsp

<form action="user/list.do" method="post">  
    number:<input type="text" name="userId" />  
    userName:<input type="text" name="userName" />  
    userSex:<input type="text" name="userSex" />  
    <input type="submit" value="tijiao" />  
</form>  
<a href="user/list.do">listttt</a>
//注意传值的形式,表单name的属性和类的属性名字必须一致



5.一次传多个参数,首页使用复选框
<form>
math:<input type="checkbox" name="userXq" value="math"/> 
    chinese:<input type="checkbox" name="userXq" value="chinese"/>
    englisher:<input type="checkbox" name="userXq" value="englisher"/>
    <input type="submit" value="tijiao" />    
</form> 

在class UserInfo中添加一个属性
private String[] userXq;
public String aa;
生成其get与set方法
新加方法(在控制器内调用)
public String toString(){
aa=Arrays.toString(userXq);
System.out.println(aa);
return aa;
}

控制器内
@RequestMapping(value="/user/list.do")
public String add1(UserInfo user){
System.out.println("user:"+user);
System.out.println("userId:"+user.getUserId());
System.out.println("userName:"+user.getUserName());
System.out.println("userSex:"+user.getUserSex());
System.out.println("userXq1:"+user.getUserXq());
System.out.println("userXq2:"+user.toString());
user.toString();
return "users/user-list";
//与传递单个参数区别不大