不加 public void validate()就好好的,加了就报错。郁闷!报错信息太长,我放在第二楼。程序代码虽长,但是入门级的程序,照着视频做的。
PointAction.java:
package test;import java.util.Date;import test2.Point;import com.opensymphony.xwork2.ActionSupport;public class PointAction extends ActionSupport {
 private Point point;
 private Point point2;
 private Point point3;
 private int age;
 private String username;
 private Date date;
 @Override
 public void validate() {
  // TODO Auto-generated method stub
   if(null==this.getUsername()||username.length()<6||username.length()>10)
   {
   addFieldError("username","username required");
   }
   
 }
@Override
public String execute() throws Exception {
 return "success";
 
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2 = point2;
}
public Point getPoint3() {
return point3;
}
public void setPoint3(Point point3) {
this.point3 = point3;
}
}
struts.xml:
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><!-- Configuration for the default package. -->
    <package name="struts2" extends="struts-default">        
    
    <action name="Login" class="test.Login">
        <result name="success">/success.jsp</result>
        <result name="input">/index.jsp</result>
        <result name="failer">/index.jsp </result>
   </action>
    <action name="PointConverter" class="test.PointAction">
    <result name ="input">/input.jsp </result>
    <result name ="success">/output.jsp</result>
    </action>
    </package>
</struts>
Point.java:
package test2;
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
PointConverter.java:
package test2;import java.util.Map;import ognl.DefaultTypeConverter;public class PointConverter extends DefaultTypeConverter { @Override
public Object convertValue(Map context, Object value, Class toType) {
// TODO Auto-generated method stub
 
if(Point.class==toType)
{

Point point = new Point();
String[] str = (String[])value;
String[]paraValues = str[0].split(",");
int x = Integer.parseInt(paraValues[0]);
int y = Integer.parseInt(paraValues[1]);
point.setX(x);
point.setY(y);
return point;
}
if(String.class==toType)
{
Point point =(Point)value;
int x = point.getX();
int y = point.getY();
String result = "[x=" + x + "," + "y=" + y + "]";
return result;
}
return null;
}}
input.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"
 %>
<%
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 'input.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body> 
    <h3><font color="red">
    使用逗号将点的两个坐标分隔开
    </h3>
    <s:actionerror/> 
    <s:form action="PointConverter">
    <s:textfield name="point" label="point">
    </s:textfield>
       <s:textfield name="point2" label="point2">
    </s:textfield>
       <s:textfield name="point3" label="point3">
    </s:textfield>
     <s:textfield name="age" label="age">
    </s:textfield>
    <s:textfield name="username" label="username">
    </s:textfield>
    <s:textfield name="date" label="birthday">
    </s:textfield>
    <s:submit label ="submit"></s:submit>
    
    </s:form>
    <br>
  </body>
</html>output.jsp:
<%@ 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 'output.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body>
   point:<s:property value="point"/> <br>
   point2:<s:property value="point2"/> <br>
   point3:<s:property value="point3"/> <br>
   age:<s:property value="age"/><br>
   username:<s:property value="username"/><br>
   date:<s:property value="date"/>
  </body>
</html>input.jsp:<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"
 %>
<%
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 'input.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body> 
    <h3><font color="red">
    使用逗号将点的两个坐标分隔开
    </h3>
    <s:actionerror/> 
    <s:form action="PointConverter">
    <s:textfield name="point" label="point">
    </s:textfield>
       <s:textfield name="point2" label="point2">
    </s:textfield>
       <s:textfield name="point3" label="point3">
    </s:textfield>
     <s:textfield name="age" label="age">
    </s:textfield>
    <s:textfield name="username" label="username">
    </s:textfield>
    <s:textfield name="date" label="birthday">
    </s:textfield>
    <s:submit label ="submit"></s:submit>
    
    </s:form>
    <br>
  </body>
</html>

解决方案 »

  1.   

    HTTP Status 500 - 
    ________________________________________
    type Exception report
    message 
    description The server encountered an internal error () that prevented it from fulfilling this request.
    exception 
    org.apache.jasper.JasperException: Class: org.apache.struts2.components.template.TemplateEngineManager
    File: TemplateEngineManager.java
    Method: getTemplateEngine
    Line: 102 - org/apache/struts2/components/template/TemplateEngineManager.java:102:-1
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
    root cause 
    Class: org.apache.struts2.components.template.TemplateEngineManager
    File: TemplateEngineManager.java
    Method: getTemplateEngine
    Line: 102 - org/apache/struts2/components/template/TemplateEngineManager.java:102:-1
    org.apache.struts2.components.UIBean.end(UIBean.java:515)
    org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)

    root cause 
    java.lang.NullPointerException
    org.apache.struts2.components.template.TemplateEngineManager.getTemplateEngine(TemplateEngineManager.java:102)
    org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:549)
    org.apache.struts2.components.UIBean.end(UIBean.java:513)
    org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    org.apache.jsp.input_jsp._jspx_meth_s_005ftextfield_005f0(input_jsp.java:234)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
    ________________________________________
    Apache Tomcat/6.0.20
      

  2.   

    这个应该没关系的。不用看。
    login.java:
    package test;import com.opensymphony.xwork2.ActionSupport;public class Login extends ActionSupport{
       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;
    }
    @Override
    public void validate() {
    // TODO Auto-generated method stub
     if(null==this.getUsername()||"".equals(this.getUsername()))
     {
     addFieldError("username","username required");
     }
     if(null==this.getPassword()||"".equals(this.getPassword()))
     {
     addFieldError("password","password required");
     }  
    }
    @Override
    public String execute() throws Exception {
    // TODO Auto-generated method stub
    if("hello".equals(this.getUsername())&&("world".equals(this.getPassword())))
    {return "success";}
    else
    {
     addFieldError("username","username or password error");
    return "failer";}
    }}
      

  3.   

    直接访问,input.jsp就是第一个界面。login.java类是另一个项目的。只不过放到一个文件家里了
      

  4.   

    input.jsp通过框架调pointAction,然后pointAction自动被PointConverter校验,之后返回output.jsp
      

  5.   

    进入input.jsp,输入什么值报错!
      

  6.   

    在 :validate() 和 execute()两个方法前都加上 断点,肯定先执行:validate() 然后看哪儿抱错了不加 public void validate()就好好的,加了就报错 --》 你加上 @Override看你的:java.lang.NullPointerException 好象在 input.jsp 没有输入什么值...
      

  7.   

    在输入的username长度小于6,符合校验条件的时候,报错
      

  8.   

    将:
    if(null==this.getUsername()||username.length() <6||username.length()>10)
    {
    addFieldError("username","username required");

    替换成:
    if(null==this.getUsername()||username.length() <6||username.length()>10)
    {
    addActionError("username","username required");