我试图在struts中验证表单,但是每当我使用validate方法时,它都会显示“ Struts已检测到未处理的异常:”,下面会提到
如果我不能验证它是否运行良好。我附上了我使用的文件。
动作课
import com.opensymphony.xwork2.ActionSupport;
This is the action class that contains the "validate" method for validating.
public class RegistrationAction extends ActionSupport {
private static final long serialVersionUID = 1L;
Form form;
@Override
public String execute() throws Exception {
System.out.println(form);
return SUCCESS;
}
public Form getForm() {
return form;
}
public void setForm(Form form) {
this.form = form;
}
@Override
public void validate() {
if(form.getFirstname().equals(""))
addFieldError("form.firstname", "invalid name");
}
}
Form.java
public class Form {
private String firstname;
private String lastname;
private String gender;
private String age;
private String email;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Form {firstname:'" + firstname + "', lastname:'" + lastname + "', gender:'" + gender + "', age:'" + age
+ "', email:'" + email + "}";
}
}
struts.xml
<struts>
<constant name="struts.devMode" value="true"/>
<package name="helloworld" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="hello"
class="com.example.struct.HelloWorldAction" method="execute">
<result name="success">/hello.jsp</result>
</action>
<action name="testAction" class="com.example.struct.TestAction" method="execute">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<package name="registration" extends="struts-default">
<action name="reg">
<result>/registration.jsp</result>
</action>
<action name="register"
class="com.example.registration.RegistrationAction" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struct_example</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
resgistration.jsp(注册表格)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Register here</title>
</head>
<body>
<s:form action="register">
<s:textfield name="form.firstname" label="First Name"></s:textfield>
<s:textfield name="form.lastname" label="Last Name"></s:textfield>
<s:radio name="form.gender" list="{'Male','Female'}" label="Gender" />
<s:textfield name="form.age" label="Age"></s:textfield>
<s:textfield name="form.email" label="Email"></s:textfield>
<s:submit value="Register"></s:submit>
</s:form>
</body>
</html>
没有验证方法,它运行良好。.