Struts和SpringMVC传递参数比较

struts和springmvc默认均是使用的请求转发进行参数传递

SpringMVC:

jsp代码:

1.jsp

<div>
	<form action="/login">
		<input type="text" placeholder="用户名" name="username"><br/>
		<input type="text" placeholder="密码" name="password"><br>
		<input type="submit" value="登录">
	</form>
</div>

2.jsp

<div>
	<form action="/login">
		<input type="text" placeholder="用户名" name="user.username"><br/>
		<input type="text" placeholder="密码" name="user.password"><br>
		<input type="submit" value="登录">
	</form>
</div>

1、方法参数直接传递

	@RequestMapping("login1")
	public String demo02(String username,String password){
		System.out.println(username+"-----"+password);
		return "success.jsp";
	}

2、方法对象传递时,1.jsp和2.jsp均可(与struts的强制要求不同)

	@RequestMapping("login1")
	public String demo02(User user){
		System.out.println(user.getUsername()+"-----"+user.getPassword());
		return "success.jsp";
	}

3、Model传递

	@RequestMapping("login3")
	public String demo04(Model model,User user){

		model.addAttribute("user", user); 
		return "success.jsp";

4、ModeAndView传递

@RequestMapping("login2")
	public ModelAndView demo03(User user){
		ModelAndView mav = new ModelAndView("success.jsp");
		mav.addObject(user);
		return mav;
	}

 

Struts:

1、基本参数属性传递

      在Action中设置相关属性,默认和请求传递参数名相同,使用set方法对相关属性进行设值

Action代码:

public class DemoAction {
	private String username;
	private String password;
	
	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 login(){
		System.out.println("执行login");
		return "login";
	}
}

jsp代码:

<div>
	<form action="/formLogin">
		<input type="text" placeholder="用户名" name="username"><br/>
		<input type="text" placeholder="密码" name="password"><br>
		<input type="submit" value="登录">
	</form>
</div>

2、对象属性传递

      在Action中设置相关对象属性,默认对象属性和请求传递参数名(对象名.属性名)必须对应(请求参数名不能直接是对象的属性,需要主动实现类型转换器,此处和springmvc不同),使用set方法对相关属性进行设值

 

Action代码:

public class FormAction{
	private User user;
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String login(){
		return "main";
	}
	
}

jsp代码:

<div>
	<form action="/formLogin">
		<input type="text" placeholder="用户名" name="user.username"><br/>
		<input type="text" placeholder="密码" name="user.password"><br>
		<input type="submit" value="登录">
	</form>
</div>

3、Action实现ModelDriven接口传递,需要实现getModel()方法

Action代码:

public class FormAction implements ModelDriven<User>{
	private User user = new User();
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String login(){
		return "main";
	}

	@Override
	public User getModel() {
		return user;
	}
	
}

jsp代码:

<div>
	<form action="/formLogin">
		<input type="text" placeholder="用户名" name="user.username"><br/>
		<input type="text" placeholder="密码" name="user.password"><br>
		<input type="submit" value="登录">
	</form>
</div>

 

posted @ 2023-05-21 21:29  Steven-Russell  阅读(14)  评论(0编辑  收藏  举报  来源