vo.java
表单元数
formbean 方
list voList = new ArrayList();
ok
表单元数
formbean 方
list voList = new ArrayList();
ok
解决方案 »
- struts1.0乱码(不安全字符)
- 关于Tomcat,帮忙看看,谢谢!
- 怎样才能在html中的textarea中显示红色字体,图象和超链接呢?
- 删除连接中的pId值为何会重复
- JSTL 如何抛出异常
- 自定义函数的问题?
- cookie传值出错,帮忙!
- 刚进公司,让我写个接口,还要把数据库查询写成配置文件,求救啊
- 很奇怪,servlet中的session取值问题,请教,
- 用Servlet写了个BO单点登录 但是死在了IEnterpriseSession enterpriseSession获取参数的地方 求解!!!!!
- jsp入门问题,请解释一下这是什么意思:<html:form method="post" action="/Login.do" name="submitForm" type="logon.form.LogonForm">
- 请高手帮忙,JS的marquee问题,急,谢谢谢
看得不大明白,可以详细些吗?
谢谢,这不失为一个好方法!
但我想知道Struts有为这种常见情况提供方案吗
String att2;
String att3;getAtt1
setAtt1
这不是限定了一定是3个元素吗
</logic: iterate>
以后改也好改
比如说
我先这么复制他30个属性
用到几个就改几个
要是 还满足不了你的需求
那就用动态actionform吧
DynaActionForm
http://blog.msnfans.com/foxgem/archives/1913.html
<设计方案>(1)JSP代码: 从数据库采集来的collection使用iterate标签显示输出
<html:form action="/beanAction">
<logic:iterate id="list" name="yourActionForm" property="list" type="xxx" >
<html:multibox name="list" property="selected">
<bean:write name="list" property="userID"/>
</html:multibox>
</html:form>(2)ActionForm代码:把每个用户的信息串个串~(把我的心~ 你的心 ~串成串~)acount是数组包含你要显示的属性,arraylist把他们连接起来.(我理解为这个是类似于vector的2维)public class YourActionForm extends ValidatorForm{
private Account[] accountList;
public Account[] getAccountList(){
return accountList;
}
public void setAccountList(Account[] accountList){
this.accountList = accountList;
}
}(3)Action代码:使用到
public class YourAction extends Action{ public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
{
YourActionForm yourActionForm = (YourAccountActionForm)actionForm;
javax.sql.DataSource dataSource = (javax.sql.DataSource)this.servlet.getServletContext().getAttribute(....DATASOURCE_KEY);
Account[] list;
try
{
连接数据库,执行访问操作 得到collection,通过名叫accountbean里的 getAllUser方法去做.
java.sql.Connection connection = dataSource.getConnection();
list = Account.getAllUser(connection);
}
catch(Exception exception)
{
设置
}
yourActionForm.setAccountList(accountList); return new ActionForward("/yourjsp.jsp");
}
}(4)Account代码:放置数组account声明,包含你的list里每条信息的所有属性~~~~(checkbox.list等)
public class Account
{
public Account() {
} private String selected;
private String list;
private String password;
private String userName;
.......................
想有什么属性就都写上吧
private java.sql.Connection connection;
public Account(String selected, String list, String password, String userName, String address, String userType) {
this.selected = selected;
...................
}
public String getSelected()
{
return selected;
}
public void setSelected(String selected)
{
this.selected = selected;
}
public String getUserID()
{
return userID;
}
public void setUserID(String userID)
{
this.userID = userID;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getUserType()
{
return userType;
}
public void setUserType(String userType)
{
this.userType = userType;
}
/**
* Set the sql connection,this method shoud be called before <strong>isExist</strong>, <strong>newAccount</strong>, <strong>loadAccount</strong> and <strong>saveAccount</strong>
* @param connection sql connection
*/
public void setConnection(java.sql.Connection connection)
{
this.connection = connection;
} /**
* Check whether such account exists
* @return true if exists, else false
*/
public String isExist()
{
try
{
String sql = "SELECT * FROM MA_USER WHERE USERID='"+ this.userID + "' AND PASSWORD='" + this.password + "'";
java.sql.Statement statement = this.connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
if(resultSet.next())
{
String userType = resultSet.getString("USERTYPE");
if(userType.equals("")){
String noexit = "none";
return noexit;
}else if(userType.equals("administrator")){
String adminexit = "administrator";
return adminexit;
}else{
String guestexit = "guest";
return guestexit;
}
}
resultSet.close();
}
catch(Exception exception)
{
exception.printStackTrace();
}
String databasestatus="disconnected";
return databasestatus;
} 从数据库取得collection的方法getAllUser
public static Account[] getAllUser(Connection connection) throws SQLException
{
String sql="SELECT * FROM YOUTABLE";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ArrayList arrayList = new ArrayList(); while(resultSet.next())
{
String selected = "";
String userID = resultSet.getString("USERID");
.......................
Account account = new Account(selected,.....);
把你的心我的心\串成串\串成串
arrayList.add(account);
}
返回你的串就是了~~~ 然后actionform里就可以给你的iterate设置值拉,说的直饿,晚上吃羊肉串去.
return (Account[])arrayList.toArray(new Account[arrayList.size()]);
}}
在我的项目中,我都是用我说的方法,这样才是真正灵活的办法。不过我也一直想知道是否有更好的解决方案。
我理解不了您的代码啊。
formBean里为什么没有与表单元素的property对应的field?
multibox还好办,它自动就会搜寻可以不定元素的array与之相对应,
像<html:select>,<html:text>这些,如果不能确定数目的话没法设计formBean中字段的啊to tomuno(tomuno):
当然不能一一手动修改啊,元素数目完全就是动态的,总不能一次次的改吧;
至于DynaActionForm,第一次接触,看来还要好好研究一下看能否用得上。to s_phoenix() :
hand~~看来不得已还是要用您提出的那种解析字符串方法了!
我只是想学习一下Struts有没有更自然一点的解决方案。to sonsan() :
您理解错我的意思了!
formBean并不是把信息传到页面的formBean,
而是表单提交后用于传递数据到Action的formBean