不加 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>
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>
解决方案 »
- 关于引用外部JS文件的问题
- JSP中字段验证问题
- jsp开发一个新闻管理项目 ,在后台不知道该如何写数据库管理和数据库备份的代码!
- 用struts作的网站的发布问题
- 看了一个开源的网站,写的是不错,但是不会调试
- CSDN难道就没有懂这的人吗?帮我看下是循环还是哪里不对?在线等待,马上结贴
- 100分急求jsp自动发送邮件程序。
- 麻烦各位帮帮小弟忙
- jsp连接oracle的数据源配置成功,jsp页面为什么找不到数据源?
- HTTP Status 500 - 求大神解决,万分感谢 急
- 请教:多个checkbox值传递到其它servlet,再将值传回checkbox
- Can't connect to MySQL server on '127.0.0.1'
________________________________________
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
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";}
}}
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");
}