不显示错误信息

jxhyang 2011-03-13 11:26:21
以下为structs框架,一个登陆验证,但是当他成功时并没用成功跳转到suc.jsp页面,而且,当没有输入用户名时<html:errors property="username"/>显示不能并排显示出来,就是说跟在文本框后面显示,以下代码我注释了,但是假设作为全局的错误<font color="red"><html:errors/></font>又可以显示出来,我初学structs,不知道这是为什么,希望大家能帮忙看看,代码虽然长,但是关键的就那几个,麻烦看看
login.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<html:base />

<title>login.jsp</title>

</head>

<body>
<font color="red"><html:errors/></font><!-- 只有此代码有用 -->
<!-- <font color="red"><html:errors property="loginerror"/></font>假设去掉注释此代码,好像没有显示错误信息-->
<html:form action="login.do" method="post" >
<table border="0">
<tr>
<td>用户名</td>
<td><html:text property="username" /></td>
<!-- <td><html:errors property="username"/></td>-->
</tr>
<tr>
<td>密码</td>
<td><html:password property="password" /></td>
<!-- <td><html:errors property="password"/></td> 同理-->
</tr>
<tr>
<td colspan="2" align="center"><html:submit value="提交"/></td>
</tr>
</table>
</html:form>
</body>
</html:html>
loginAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.yourcompany.struts.form.LoginForm;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
if(loginForm.getUsername().equals("admin")&&loginForm.getUsername().equals("123"))
{
//return new ActionForward("/suc.jsp");
request.getSession().setAttribute("uname",loginForm.getUsername());
return mapping.findForward("suc");
}else

{
ActionMessages errors=new ActionMessages();
errors.add("loginerror", new ActionMessage("loginerrors"));
this.addMessages(request, errors);
return mapping.getInputForward();
}
//return null;
}
}
loginForm.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginForm extends ActionForm {
private String username;
private String password;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors error=new ActionErrors();
if(this.username==null||this.username.trim().equals(""))
{
error.add(username, new ActionMessage("username.null"));
}
if(this.password==null||this.password.trim().equals(""))
{
error.add(password, new ActionMessage("password.null"));
}
return error;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
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;
}
}
suc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

</head>

<body>
成功 <br>
</body>
</html>
resource文件
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project structslogin

username.null=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A<br>
password.null=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A<br>
loginerrors=\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF<br>
structs.xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.yourcompany.struts.form.LoginForm" />

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="com.yourcompany.struts.action.LoginAction">
<forward name="suc" path="/suc.jsp" />
</action>

</action-mappings>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

...全文
97 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
失去乐山贼 2011-03-13
loginForm.getPassword没写
回复
失去乐山贼 2011-03-13
if (loginForm.getUsername().equals("admin")
&& loginForm.getUsername().equals("123")) {
回复
失去乐山贼 2011-03-13
LoginForm类
没加引号,最好用get方法取值 this.getUsername==null;
if (this.username == null || this.username.trim().equals("")) {
error.add("username", new ActionMessage("username.null"));
}
if (this.password == null || this.password.trim().equals("")) {
error.add("password", new ActionMessage("password.null"));
}
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2011-03-13 11:26
社区公告
暂无公告