日期:2014-05-19  浏览次数:20707 次

strut2转换器问题
Java code


//Login.jsp
<s:form action="Login.action" theme="simple" namespace="/">
  <table border="0" style="width: 600px;" cellpadding="5">
  <tr>
<th colspan="2" width="400px">
<h1><label>Login System</label></h1>
</th>
<td width="200px">

</td>
  </tr>
  <tr>
<td>
<label style="font-size: large;">User&nbsp;&nbsp;&nbsp;    ID:</label>
</td>
<td>
<s:textfield name="userid" cssStyle="font-size: large; width: 230px"></s:textfield>
</td>
<td rowspan="3">

</td>
</tr>
<tr>
<td>
<label style="font-size: large;">Password:</label>
</td>
<td>
<s:password name="password" cssStyle="font-size: large; width: 230px"></s:password>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<s:submit value="提交" cssStyle="font-size: large;"></s:submit>&nbsp;&nbsp;&nbsp;&nbsp;
</td>
  </table>
</s:form>

//用form提交
struts.xml
<action name="Login" Class="ActionForm.LoginAction">
  <result name="success">/index.jsp</result>
  <result name="input">/Login.jsp</result>
</action>


//LoginAction.java


private int userid;
private int password;
private Connection conn = null;
private Statement sm = null;
private ResultSet rs = null;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}


@Override
public String execute() throws Exception {
int i = login(userid, password);
if(i >0)
{
return SUCCESS;
}
else
{
return INPUT;
}
}


public int login(int userid,String password) throws SQLException
{
int i = 0;
StringBuffer sb = new StringBuffer();
sb.append("select uname from login_user_Table where uid='").append(userid).append("' and upassword='").append(password).append("'");
System.out.println(sb.toString());
try
{
DBConnection db = new DBConnection();
conn = db.getConnection();
sm = conn.createStatement();
rs = sm.executeQuery(sb.toString());
if(rs.next())
{
i = 1;
}
else
{
i=0;
}
}
catch (Exception e) {
e.printStackTrace();
}
return i;
}


我要将uname 返回到主界面,请问能在LoginConverter.java转换器里面转换吗?还有没有更好的方法

------解决方案--------------------
没看多大明白你要做什么,
如果只是为了要显示出你从数据库中查询出来的uname,最简单的办法:
在你的Action中加个成员变量如:private String uname;及其uname的get/set方法,
在你查询出uname后,把值赋给变量uname。
在页面就可以用struts标签或el表达式取出来了。。。。。
<s:property value="uname" />或${uname}