为了熟悉<html:errors>标签的用法,我在下面的一个简单例子上想用这个标签验证表单数据,如果用户名或者密码为空时,就在index.jsp表单上显示对应的错误信息。
以下代码:
(一)index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html:html>
 <head><title>test</title></head>
 <body>  <html:form action="test.do">
   username:<html:text property="username"/><html:errors property="username" /><br>
   password:<html:text property="password"/><html:errors property="password"/><br>
   <html:submit>Submit</html:submit>
   <html:reset>Reset</html:reset>
   <html:cancel>Cancel</html:cancel>
  </html:form>
 </body>
</html:html>(二)TestForm
package pyj.com.forms;
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 TestForm extends ActionForm {
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 void reset(ActionMapping mapping,HttpServletRequest request){
this.username = null;
this.password = null;

}

public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
ActionErrors errors = new ActionErrors();
if((username==null)||(username).length()<1){
errors.add("username",new ActionMessage("error.username.required"));
}
else if((password==null)||(password).length()<1){
errors.add("password",new ActionMessage("error.password.required"));
}
return errors;
}}(三)TestAction
package pyj.com.actions;
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 pyj.com.forms.TestForm;
public class TestAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
TestForm tf=(TestForm)form;
String username=tf.getUsername();
String password=tf.getPassword();
if(username=="admin"&&password=="admin"){

return mapping.findForward("testright");

}
}else return mapping.findForward("testwrong");}
(四)struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
       
<struts-config>
  <form-beans>
    <form-bean name="TestForm" type="pyj.com.actions.TestForm"></form-bean>
  </form-beans>  <action-mappings>
    
    <action path="/test"
            type="pyj.com.actions.TestAction"
            name="TestForm"
       >
       <forward name="testright" path="/tr.jsp"></forward> 
       <forward name="testwrong" path="/index.jsp"></forward>    
    </action>
  
  </action-mappings>
   <message-resources parameter="res.MessageResources" />
</struts-config>(五)在src下的res.MessageResources.properties
# -- standard errors --
error.username.required=\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a
error.password.required=\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a
运行结果出错:其中下面的javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve mapping for action /Testform是指我在action的配置出错吗?错在哪里了?除了这个自外还有其他错吗?怎么样改才能实现<html:errors>的功能?顺便问问,谁能给我结合<html:errors>用法的简单例子吗?不胜感激!谢谢!
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 107:  <head><title>test</title></head>
8:  <body>
9: 
10:   <html:form action="Testform.do">
11:    username:<html:text property="username"/><html:errors property="username" /><br>
12:    password:<html:text property="password"/><html:errors property="password"/><br>
13:    <html:submit>Submit</html:submit>root cause javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot retrieve mapping for action /Testform
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.index_jsp._jspService(index_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

解决方案 »

  1.   

    7: <head><title>test</title></head>
    8: <body>
    9: 
    10: <html:form action="Testform.do">
    11: username:<html:text property="username"/><html:errors property="username" /><br>
    12: password:<html:text property="password"/><html:errors property="password"/><br>
    13: <html:submit>Submit</html:submit>10明显有问题嘛!
      

  2.   

    将 <html:form action="Testform.do">
    改成 <html:form action="test.do">
    因为你在struts.xml文件中
    <action path="/test"
    type="pyj.com.actions.TestAction"
    name="TestForm"
    >
    <forward name="testright" path="/tr.jsp"></forward> 
    <forward name="testwrong" path="/index.jsp"></forward> 
    </action>
    已经把TestAction映射为test了
      

  3.   

    我在index.jsp中的<html:form action="/test.do",在struts-config.xml里面的<ation path="/test".../>只是运行后报错生成
    7: <head><title>test</title></head>
    8: <body>
    9: 
    10: <html:form action="Testform.do">
    11: username:<html:text property="username"/><html:errors property="username" /><br>
    12: password:<html:text property="password"/><html:errors property="password"/><br>
    13: <html:submit>Submit</html:submit>