struts2输入校验得不出结果 求指点
输入界面:<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="testValidate" method="post">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:password name="repassword" label="repassword"></s:password>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="birthday" label="birthday"></s:textfield>
<s:textfield name="graduation" label="graduation"></s:textfield>
<s:submit name="submit"></s:submit>
<s:reset name="reset"></s:reset>
</s:form>
</body>
</html>
action处理代码:
package struts2;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class ValidateTest extends ActionSupport
{
private String username;
private String password;
private String repassword;
private int age;
private Date birthday;
private Date graduation;
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 String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getGraduation() {
return graduation;
}
public void setGraduation(Date graduation) {
this.graduation = graduation;
}
public String execute() throws Exception
{
return SUCCESS;
}
public void validate()
{
System.out.println("已经调用了效验方法");
if(null == username || username.length() <6 || username.length() >10)
{
this.addFieldError("username","username needs 6 to 10 chars");
}
if(null == password || password.length() <6 || password.length() >10)
{
this.addFieldError("password", "password needs 6 to 10 chars");
}
if(null == repassword || repassword.length() <6 || repassword.length() >10)
{
this.addFieldError("repassword", "repassword needs 6 to 10 chars");
}
if( null != password || null !=repassword)
{
if( !password.equals(repassword) )
{
this.addFieldError("repassword", "two pasword should be same");
}
}
}
}
struts.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="com" extends="struts-default" namespace="/">
<action name="testValidate" class="struts2.ValidateTest">
<result name="input">regist.jsp</result>
<result name="success">success.jsp</result>
</action>
</package>
</struts>
success.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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%>">
<title>My JSP 'success.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
检验正确 <br>
</body>
</html>
点击提交时出现错误信息:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
struts2.ValidateTest.execute(ValidateTest.java:13)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
[解决办法]
struts2输入校验得不出结果,那你现在出现什么的效果了?
[解决办法]
空指针错误~
[解决办法]
楼主有整个文件代码不,这样看很累啊~
[解决办法]
struts2.ValidateTest.execute(ValidateTest.java:13)
看看你这一行的代码。
[解决办法]
楼主可以找我,我帮你看看代码~
[解决办法]
建议使用JS效验
[解决办法]
NullPointerException o(︶︿︶)o
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html