struts2中从页面取值的三种方式
来源:互联网 发布:关明生为什么离职知乎 编辑:程序博客网 时间:2024/05/22 10:24
在图解:在Eclipse中建立第一个Struts2程序中我们建立了第一个struts程序,那么如何把登陆页面中的用户名传递到登录成功的页面中呢?
有三种方式,
1,使用默认的action的传递方式。
2,自定义一个vo,在action中使用这个vo
3,使用ModelDriven的方式。
下面分别叙述。
1,使用默认的action的传递方式。
action文件如下:
package struts2.login;
public class LoginAction {
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 execute() {
System.out.println (LoginAction.class.hashCode());
if (username.equalsIgnoreCase("aaa") &&
password.equals("aaaaaa")) {
return "loginSuc";
}
else {
return "loginFail";
}
}
}
登陆成功的文件如下:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
欢迎您,<s:property value="username" />登录成功。
2,自定义一个vo,在action中使用这个vo
自定义vo文件名:LoginVO.java
文件内容:
package struts2.login;
public class LoginVO {
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;
}
}
在Action文件中,要使用这个vo
文件内容:
package struts2.login;
public class LoginAction {
private LoginVO user = null;
public String execute() {
System.out.println (LoginAction.class.hashCode());
if (user.getUsername().equalsIgnoreCase("aaa") &&
user.getPassword().equals("aaaaaa")) {
return "loginSuc";
}
else {
return "loginFail";
}
}
public LoginVO getUser() {
return user;
}
public void setUser(LoginVO user) {
this.user = user;
}
}
登陆成功的文件如下:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
欢迎您,<s:property name="user.username">登录成功。
注意login文件的部分也要进行修改
文件内容如下:
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<title>login2</title>
<form action="login.action" method="post">
username:<input type="input"name="user.username"><br>
password:<input type="input"name="user.password"><br>
<input type="submit" value="登录">
</form>
3,使用ModelDriven的方式。
同样也需要一个vo,这个vo和方法2中的一致,但是action中的写法就不一样了。
action文件内容如下:
package struts2.login;
import com.opensymphony.xwork2.ModelDriven;
public class LoginActionimplements ModelDriven<LoginVO>{
@Override
public LoginVO getModel() {
// TODO Auto-generated method stub
return user;
}
private LoginVO user = new LoginVO();
public String execute() {
System.out.println (LoginAction.class.hashCode());
if (user.getUsername().equalsIgnoreCase("aaa") &&
user.getPassword().equals("aaaaaa")) {
return "loginSuc";
}
else {
return "loginFail";
}
}
}
而登陆成功的页面和login的文件则不需要追加user的前缀,即和方法1的文件内容一样。
三种方法的总结:
第一种方法把form的值都放在action文件中,当form提交的项目很多的时候,action的内容将变得很多,很臃肿。项目少的时候可用。
第二种方法将form的值单独放在vo中,解决了action文件臃肿的问题,同时使form和action分开,较好。但是需要在设置和获取的jsp页面上进行标识。
第三种方法在第二种方法的基础上,通过实现特定的接口,去掉了action中的set和get方法,同时去掉了jsp页面上的标识。使后台程序的logic更加清晰。
有三种方式,
1,使用默认的action的传递方式。
2,自定义一个vo,在action中使用这个vo
3,使用ModelDriven的方式。
下面分别叙述。
1,使用默认的action的传递方式。
action文件如下:
package struts2.login;
public class LoginAction {
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 execute() {
System.out.println (LoginAction.class.hashCode());
if (username.equalsIgnoreCase("aaa") &&
password.equals("aaaaaa")) {
return "loginSuc";
}
else {
return "loginFail";
}
}
}
登陆成功的文件如下:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
欢迎您,<s:property value="username" />登录成功。
2,自定义一个vo,在action中使用这个vo
自定义vo文件名:LoginVO.java
文件内容:
package struts2.login;
public class LoginVO {
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;
}
}
在Action文件中,要使用这个vo
文件内容:
package struts2.login;
public class LoginAction {
private LoginVO user = null;
public String execute() {
System.out.println (LoginAction.class.hashCode());
if (user.getUsername().equalsIgnoreCase("aaa") &&
user.getPassword().equals("aaaaaa")) {
return "loginSuc";
}
else {
return "loginFail";
}
}
public LoginVO getUser() {
return user;
}
public void setUser(LoginVO user) {
this.user = user;
}
}
登陆成功的文件如下:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
欢迎您,<s:property name="user.username">登录成功。
注意login文件的部分也要进行修改
文件内容如下:
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<title>login2</title>
<form action="login.action" method="post">
username:<input type="input"name="user.username"><br>
password:<input type="input"name="user.password"><br>
<input type="submit" value="登录">
</form>
3,使用ModelDriven的方式。
同样也需要一个vo,这个vo和方法2中的一致,但是action中的写法就不一样了。
action文件内容如下:
package struts2.login;
import com.opensymphony.xwork2.ModelDriven;
public class LoginActionimplements ModelDriven<LoginVO>{
@Override
public LoginVO getModel() {
// TODO Auto-generated method stub
return user;
}
private LoginVO user = new LoginVO();
public String execute() {
System.out.println (LoginAction.class.hashCode());
if (user.getUsername().equalsIgnoreCase("aaa") &&
user.getPassword().equals("aaaaaa")) {
return "loginSuc";
}
else {
return "loginFail";
}
}
}
而登陆成功的页面和login的文件则不需要追加user的前缀,即和方法1的文件内容一样。
三种方法的总结:
第一种方法把form的值都放在action文件中,当form提交的项目很多的时候,action的内容将变得很多,很臃肿。项目少的时候可用。
第二种方法将form的值单独放在vo中,解决了action文件臃肿的问题,同时使form和action分开,较好。但是需要在设置和获取的jsp页面上进行标识。
第三种方法在第二种方法的基础上,通过实现特定的接口,去掉了action中的set和get方法,同时去掉了jsp页面上的标识。使后台程序的logic更加清晰。
- struts2中从页面取值的三种方式
- struts2中从页面取值的三种方式
- struts2中从页面取值的三种方式
- struts2从form取值的三种方式
- struts2从表单取值的三种方式
- struts2从form取值的三种方式
- struts2从请求取值的三种方式
- struts2从form取值的三种方式
- struts2从表单取值的三种方式
- struts2从form取值的三种方式
- struts2从form取值的三种方式
- struts2页面取值方式及url,if的用法
- struts 3种从jsp页面 取值方式
- struts2中数据共享给jsp页面的三种方式
- struts2接收页面传参的三种方式
- Struts2页面取值
- Struts2页面取值:
- struts2页面取值
- CityEngine开发竞赛-作品常见问题分析及解答
- iOS开发之缓存(一):内存缓存
- android service生命周期
- 内部类总结
- OracleOraHome90Agent
- struts2中从页面取值的三种方式
- JSON对象存在换行符
- shell 编程手册
- Spring Annotation
- commons-dbutils的操作方法
- 搭建开发环境
- WIN7 开启vs2010自带SQL数据库 SqlExpress
- 详解 Android 的 Activity 组件
- WindowsPhone相关的几种数据收集框架的调研