您的位置:首页 > 编程语言 > Java开发

Struts2三种数据转移方式

2015-08-26 22:53 609 查看
http://blog.csdn.net/dc_726/article/details/8041861

Struts2提供了JavaBean属性,JavaBean对象,ModelDriven对象三种方式来保存HTTP请求中的参数。下面通过一个最常见的

登录的例子来看下这三种数据转移方式。页面代码很简单,提交表单中包含有用户名和密码,在Action中得到这两个参数从而

验证用户是否登录成功。

一、JavaBean属性

[html] view
plaincopy

<%@ page contentType="text/html;charset=UTF-8" %>



<html>



<head></head>



<body>

<h1>登录页</h1>



<form action="/cdai/login" method="post">



<div>

<label for="username">名称:</label>

<input id="username" name="username" type="textfield"/>

</div>



<div>

<label for="password">密码:</label>

<input id="password" name="password" type="password"/>

</div>



<div>

<label for="rememberMe">

<input id="rememberMe" name="rememberMe" type="checkbox"/> 记住我

</label>

<input type="submit" value="登录"></input>

</div>



</form>



</body>



</html>

[java] view
plaincopy

package com.cdai.web.ssh.action;



import com.cdai.web.ssh.request.LoginRequest;

import com.cdai.web.ssh.service.UserService;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ModelDriven;



public class LoginAction implements Action {



private String username;



private String password;



private UserService userService;



@Override

public String execute() {



System.out.println("Login action - " + request);



return SUCCESS;

}



public String getUsername() {

return request;

}



public void setUsername(String username) {

this.username = username;

}



public String getPassword() {

return request;

}



public void setPassword(String Password) {

this.Password = Password;

}



}

这种方式比较简明,直接将表单中的参数保存到Action中的属性中。Action在验证时可能还需要将用户名和密码再封装成Dto的

形式传给Service层进行验证。所以为什么不更进一步,直接将用户名和密码保存到Dto中。

二、JavaBean对象

[html] view
plaincopy

<%@ page contentType="text/html;charset=UTF-8" %>



<html>



<head></head>



<body>

<h1>登录页</h1>



<form action="/cdai/login" method="post">



<div>

<label for="username">名称:</label>

<input id="username" name="request.username" type="textfield"/>

</div>



<div>

<label for="password">密码:</label>

<input id="password" name="request.password" type="password"/>

</div>



<div>

<label for="rememberMe">

<input id="rememberMe" name="rememberMe" type="checkbox"/> 记住我

</label>

<input type="submit" value="登录"></input>

</div>



</form>



</body>



</html>

[java] view
plaincopy

package com.cdai.web.ssh.action;



import com.cdai.web.ssh.request.LoginRequest;

import com.cdai.web.ssh.service.UserService;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ModelDriven;



public class LoginAction implements Action {



private LoginRequest request;



private UserService userService;



@Override

public String execute() {



System.out.println("Login action - " + request);



return SUCCESS;

}



public LoginRequest getRequest() {

return request;

}



public void setRequest(LoginRequest request) {

this.request = request;

}



}

这样就可以很方便地直接调用Service层了。但是有一个小缺点就是这样加深了页面参数名的深度,只有为参数名加上request

前缀(Action中的属性名)才能使Struts2通过OGNL将表单中的参数正确保存到request对象中。

三、ModelDriven对象

[html] view
plaincopy

<%@ page contentType="text/html;charset=UTF-8" %>



<html>



<head></head>



<body>

<h1>登录页</h1>



<form action="/cdai/login" method="post">



<div>

<label for="username">名称:</label>

<input id="username" name="username" type="textfield"/>

</div>



<div>

<label for="password">密码:</label>

<input id="password" name="password" type="password"/>

</div>



<div>

<label for="rememberMe">

<input id="rememberMe" name="rememberMe" type="checkbox"/> 记住我

</label>

<input type="submit" value="登录"></input>

</div>



</form>



</body>



</html>

[java] view
plaincopy

package com.cdai.web.ssh.action;



import com.cdai.web.ssh.request.LoginRequest;

import com.cdai.web.ssh.service.UserService;

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ModelDriven;



public class LoginAction implements Action, ModelDriven<LoginRequest>

{



private LoginRequest request = new LoginRequest();



private UserService userService;



@Override

public String execute() {



System.out.println("Login action - " + request);



return SUCCESS;

}



@Override

public LoginRequest getModel() {

return request;

}



}

这种方式要多实现一个ModelDriven接口,将ModelDriven提供的对象也保存到ValueStack上,从而使前台页面可以直接通过

username和password属性名来定义表单的参数名了。

三种方式具体采用哪种不能一概而论,还是看项目的具体需求再自己定吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: