struts2为什么不显示username,其他的可以正常显示,但username这个有时不能正常显示。代码如下:
1.register.jsp <body>
<s:fielderror></s:fielderror>
<form action="register" method="post">
<table align="center" width="40%" border="1">
<tr>
<td>username</td>
<td><input name="username" type="text" value="${requestScope.username}"></td>
</tr>
<tr>
<td>password</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td>re-password</td>
<td><input name="repassword" type="password"></td>
</tr>
<tr>
<td>age</td><td>
<input name="age" type="text" value="${age}"></td>
</tr>
<tr>
<td>birthday</td><td>
<input name="birthday" type="text" value="${birthday}"></td>
</tr>
<tr>
<td>graduation</td><td>
<input name="graduation" type="text" value="${graduation}"></td>
</tr>
<tr>
<td>
<input name="submit" type="submit"></td>
<td>
<input name="reset" type="reset"></td>
</tr>
</table>
</form>
</body>
2.registerAction.java
public class RegisterAction extends ActionSupport {
private String username;
private String password;
private String repassword;
private int age;
private Date birthday;
private Date graduation;
..............
public String execute()throws Exception{
return SUCCESS;
}
public void validate()
{
System.out.println("validate~~~~~~~~~~~~~~~~~~~");
if(null == username || username.length() < 6 || username.length() > 10)
{
this.addFieldError("username","username invalid");
}
if(null == password || password.length() < 6 || password.length() > 10)
{
this.addFieldError("password","password invalid");
}
else if(null == repassword || repassword.length() < 6 || repassword.length() > 10)
{
this.addFieldError("repassword","re-password invalid");
}
else if(!password.equals(repassword))
{
this.addFieldError("password","two passwords not the same");
}
if(age < 1 || age > 150)
{
this.addFieldError("age","age invalid");
}
if(null == birthday)
{
this.addFieldError("birthday","birthday invalid");
}
if(null == graduation)
{
this.addFieldError("graduation","graduation invalid");
}
if(null != birthday && null != graduation)
{
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);
Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);
if(!c1.before(c2))
{
this.addFieldError("birthday","birthday should be before graduation");
}
}
}
3.struts.xml
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/Result.jsp</result>
<result name="failer">/login2.jsp</result>
</action>
<action name="pointConverter" class ="com.test.action.PointAction">
<result name="success">/output.jsp</result>
</action>
<action name="register" class ="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
4.success.jsp
<body>
<table align="center" width="40%" border="1">
<tr>
<td>username</td>
<td>${requeStscope.username}</td>
</tr>
<tr>
<td>password</td>
<td>${requestScope.password}</td>
</tr>
<tr>
<td>age</td><td>
${requestScope.age}</td>
</tr>
<tr>
<td>birthday</td><td>
${requestScope.birthday}</td>
</tr>
<tr>
<td>graduation</td><td>
${requestScope.graduation}</td>
</tr>
</table>
</body>
1.register.jsp <body>
<s:fielderror></s:fielderror>
<form action="register" method="post">
<table align="center" width="40%" border="1">
<tr>
<td>username</td>
<td><input name="username" type="text" value="${requestScope.username}"></td>
</tr>
<tr>
<td>password</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td>re-password</td>
<td><input name="repassword" type="password"></td>
</tr>
<tr>
<td>age</td><td>
<input name="age" type="text" value="${age}"></td>
</tr>
<tr>
<td>birthday</td><td>
<input name="birthday" type="text" value="${birthday}"></td>
</tr>
<tr>
<td>graduation</td><td>
<input name="graduation" type="text" value="${graduation}"></td>
</tr>
<tr>
<td>
<input name="submit" type="submit"></td>
<td>
<input name="reset" type="reset"></td>
</tr>
</table>
</form>
</body>
2.registerAction.java
public class RegisterAction extends ActionSupport {
private String username;
private String password;
private String repassword;
private int age;
private Date birthday;
private Date graduation;
..............
public String execute()throws Exception{
return SUCCESS;
}
public void validate()
{
System.out.println("validate~~~~~~~~~~~~~~~~~~~");
if(null == username || username.length() < 6 || username.length() > 10)
{
this.addFieldError("username","username invalid");
}
if(null == password || password.length() < 6 || password.length() > 10)
{
this.addFieldError("password","password invalid");
}
else if(null == repassword || repassword.length() < 6 || repassword.length() > 10)
{
this.addFieldError("repassword","re-password invalid");
}
else if(!password.equals(repassword))
{
this.addFieldError("password","two passwords not the same");
}
if(age < 1 || age > 150)
{
this.addFieldError("age","age invalid");
}
if(null == birthday)
{
this.addFieldError("birthday","birthday invalid");
}
if(null == graduation)
{
this.addFieldError("graduation","graduation invalid");
}
if(null != birthday && null != graduation)
{
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);
Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);
if(!c1.before(c2))
{
this.addFieldError("birthday","birthday should be before graduation");
}
}
}
3.struts.xml
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/Result.jsp</result>
<result name="failer">/login2.jsp</result>
</action>
<action name="pointConverter" class ="com.test.action.PointAction">
<result name="success">/output.jsp</result>
</action>
<action name="register" class ="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
4.success.jsp
<body>
<table align="center" width="40%" border="1">
<tr>
<td>username</td>
<td>${requeStscope.username}</td>
</tr>
<tr>
<td>password</td>
<td>${requestScope.password}</td>
</tr>
<tr>
<td>age</td><td>
${requestScope.age}</td>
</tr>
<tr>
<td>birthday</td><td>
${requestScope.birthday}</td>
</tr>
<tr>
<td>graduation</td><td>
${requestScope.graduation}</td>
</tr>
</table>
</body>
解决方案 »
- Hibernate使用Ehcache二级缓存时的异常NoCacheRegionFactoryAvailableException
- 【求助】Hibernate异常:null id *** entry
- 关于javaee的问题
- struts 1.2 批量提交
- ACEGI在ACTION中无法调用getUserName
- 连接到localhost 提示:位于 XDB 的服务器 localhost 要求用户名和密码
- 孙卫琴 精通STRUTS:基于MVC的JAVA WEB设计与开发,helloapp例子我怎么都调不通,请问有人碰到过吗?
- atomikos+spring3+openjpa2.2+mysql 集成,出现 Can't call rollback when autocommit=tru
- 100分请教一个小问题:${param.action}
- 通过复制粘贴的方式添加jar包为什么一直显示在lib目录下?
- 大年30的第一个struts2问题,,看谁比我早
- ssh搭建的一个小项目,在tomcat 启动时出问题了
直接这样应该就可以了。非要指定action名字的话,跟struts.xml里面<action name="register"
对应上
应该是<td>${requestScope.username}</td>