[转]Struts form传值
Struts form传值
大约三四个月没用过struts框架,突然想拾起来,却发现好多都忘了。出现传值传不过来的问题。没办法,上网查了一下,看见了一位老师的帖子,总结的很好。特此转载与分享,文末附链接。
在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 LoginAction implements 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更加清晰。
链接地址:http://blog.csdn.net/tianlincao/article/details/6098371
[转]Struts form传值的更多相关文章
- 基于struts研究传值问题
一.新建项目 struts 1.file——>new——>Web Project——>取名struts——>finsh——>将之前项目下的jar包copy到该项目下 2. ...
- struts form表单提交action处理之后没有跳转页面
作为一个ssm新人,对struts的理解也仅仅是书本上一些简单的示例, 对如下这个form进行提交之后,执行完action,并return 一个字符串,在struts配置文件中配置了对应的jsp页面, ...
- 【转】jsp 表单form传值
写的很好,看到了忍不住不转啊,希望可以分享一下~~ 转载自http://blog.csdn.net/anmei2010/article/details/4140216 页面间链接和数据传递的三种方式 ...
- struts jsp传值到action,乱码的解决方案
使用了Struts框架,前台写好了编码为utf-8 <%@ page language="java" contentType="text/html; charset ...
- axios实现类似form传值的格式,以及实现拦截器功能,response拦截实现权限判断
import axios from 'axios' import Qs from 'qs' // 超时设置 const service = axios.create({ transformReques ...
- struts—文件的上传和下载
设计了一个小的案例:上传图片到服务器,上传成功后显示图片列表,然后点击下载上传到服务器的图片. 注意表单的配置属性: <form enctype="multipart/Form-dat ...
- (Struts)ActionForm类及表单数据验证
LoginForm代码: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ pac ...
- struts的上传和下载
上传: jsp: <body> <h1>filogin</h1> <!--如果表单中有文件文件控件,上传的编码必须是multipart/form-data - ...
- MyEclipse+Struts+Hibernate+Mysql开发环境配置
软件: jdk-6u22-windows-x64.exe apache-tomcat-6.0.29.exe mysql-5.1.51-winx64.exe myeclipse-8.6.0-win32. ...
随机推荐
- Codeforces 903F Clear The Matrix(状态压缩DP)
题目链接 Clear The Matrix 题意 给定一个$4 * n$的矩形,里面的元素为$'.'$或$'*'$.现在有$4$种正方形可以覆盖掉$'*'$,正方形的边长分别为$1,2,3,4$. 求 ...
- 注意这几点,轻轻松松配置 Nginx + Tomcat 的集群和负载均衡
Tomcat 集群是当单台服务器达到性能瓶颈,通过横向扩展的方式提高整体系统性能的有效手段.Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,可以通过简单的配置实现 Tomcat 集群 ...
- Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想
原题链接:http://codeforces.com/contest/584/problem/D 题意: 给你一个奇数,让你寻找三个以内素数,使得和为这个奇数. 题解: 这题嘛...瞎比搞搞就好,首先 ...
- Android 分享透明图片到微信变黑的问题
/** * bitmap中的透明色用白色替换 * * @param bitmap * @return */ public static Bitmap changeColor(Bitmap bitmap ...
- Java Web开发(JSP、Servlet)乱码的一揽子解决方案
千万不要看网上那些杂七杂八的解决乱码的文章,解决乱码最好的方法是(没有之一):在所有地方统一采用UTF-8编码. 这其中包括: 1 - 工程 如果使用的是Eclipse,那么打开Preference, ...
- linux 学习解决归档管理器打开rar和zip中文文件名乱码问题
在ubunut下打开windows下压缩的rar文件和zip压缩文件出现中文文件名乱码的问题真的很头疼.文件名乱码其实也没有什么关系是不?至少重命名再改回来或者是使用英文命名都可以克服.不巧的是,如此 ...
- Can''t find the channel handler for deviceType 工行 个人网银 错误
背景描述:系统Win7,浏览器IE8.登录工商银行个人网银的时候,输入帐号密码和验证码后,出现空白页面,上面一句话 Can''t find the channel handler for devic ...
- MFC中 SDI/MDI框架各部分指针获取方式
VC MFC SDI/MDI框架各部分指针获取方式 整理总结一下,希望能帮助到别人. 获得CWinApp 获得CMainFrame 获得CChildFrame 获得CDocument 获得CV ...
- 在Delphi中应用AOP实现日志功能
AOP现在很火,网上有这许多支持AOP的框架,对于Delphi来说同样也有MeAOP.不过觉得这些框架太复杂了. 现在有一个系统,基本上都快结束了,整体上当然是没有采用什么AOP的框架.对于这样的系统 ...
- Windows下批处理命令启动项目bat脚本
文件env.cfg #server name SERVER_NAME=ActivitiService #JDK Home JDK_HOME= #Main MAIN_CLASS=com.nbtv.com ...