问题描述
当将execAndWait
拦截器与validate()
方法一起使用时,我得到了NullPointerException
.在index.jsp
中,我有一个textfield
用于firstName
.当我提交时,它首先使用validate方法在Action
类中的那个字符串validate
.然后转到execute
方法.在输出中,对于validate
方法中找不到的firstName
字段,我得到的是NullPointerException
.我知道的某个地方,execAndWait
在单独的线程中运行,这就是为什么出现此问题的原因.我想知道如何解决这个问题.明智的文件代码如下:
I am getting NullPointerException
when using execAndWait
interceptor with validate()
method. In index.jsp
, I have one textfield
for firstName
. When I do submit, it first validate
that string in Action
class with validate method. Then it goes to execute
method. In output I am getting NullPointerException
for firstName
field not found in validate
method. Somewhere I came to know that, execAndWait
runs in separate thread, that's why this problem arises. I want to know how to resolve this problem. File wise code below:
index.jsp
:
index.jsp
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:actionerror />
<s:actionmessage />
<s:form action="go" method="post">
<s:textfield name="fname" label="First-Name" />
<s:submit value="enter"></s:submit>
</s:form>
</body>
</html>
struts.xml
:
struts.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="abc" extends="struts-default">
<interceptors>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="execAndWait"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="go" class="pack.GoAction">
<interceptor-ref name="myStack" />
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
<result name="input">/index.jsp</result>
<result name="wait">/wait.jsp</result>
</action>
</package>
</struts>
GoAction.java
:
GoAction.java
:
package pack;
import java.sql.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class GoAction extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = 1L;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public User getModel() {
System.out.println("****inside getModel()****");
user=new User();
return user;
}
public void validate(){
System.out.println("****inside validate****");
if(user.getFname().length()<4){
this.addFieldError("fname", "first name can not be less than 5");
System.out.println("console: first name can not be less than 5 "+user.getFname());
}
if(user.getFname().length()==0){
this.addFieldError("fname", "first name found empty");
}
}
public String execute(){
System.out.println("****inside execute****");
String returnValue="";
int i=0;
Connection con=null;
ResultSet rs=null;
PreparedStatement ps=null;
if(user.getFname().equals("zebra")){
System.out.println("First-Name : zebra : not allowed.");
this.addActionMessage("First-Name : zebra : not allowed");
return "failure";
}
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hibernate", "hibernate");
ps=con.prepareStatement("insert into table1 values(?)");
ps.setString(1, user.getFname());
i=ps.executeUpdate();
if(i!=0){
returnValue="success";
this.addActionMessage("data successfully inserted");
System.out.println("ok");
}
else{
returnValue="failure";
System.out.println("not ok");
}
}catch(Exception ex){
System.out.println("E x c e p t i o n o c c u r r e d !!!!");
ex.printStackTrace();
}
return returnValue;
}
}
wait.jsp
:
wait.jsp
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Wait...</title>
<meta http-equiv="refresh" content="1;">
</head>
<body>
<img src="images/animation_processing.gif" />
</body>
</html>
success.jsp
:
success.jsp
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Success !!
<br>
<s:actionmessage />
</body>
</html>
输出屏幕(逐步)
但是在数据库中插入了值:
however value got inserted in Database:
控制台输出
****inside getModel()****
****inside validate****
****inside execute****
Apr 26, 2015 4:30:48 PM org.apache.struts2.util.TokenHelper warn
WARNING: Could not find token mapped to token name token
ok
****inside getModel()****
****inside validate****
Apr 26, 2015 4:30:50 PM org.apache.struts2.dispatcher.Dispatcher error
SEVERE: Exception occurred during processing request: null
java.lang.NullPointerException
at pack.GoAction.validate(GoAction.java:35)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:251)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
经过一些浏览,我知道execAndWait
在单独的线程中运行,并且无法获取user.getFname()
的值.谁能帮我解决这个问题.
After a bit of browsing, I came to know that execAndWait
runs in a separate thread and is NOT able to get the value of user.getFname()
. Can anyone help me resolving this issue.
推荐答案
更改验证方法的逻辑
public void validate(){
System.out.println("****inside validate****");
if(user.getFname() == null || user.getFname().length()==0){
this.addFieldError("fname", "first name found empty");
} else
if(user.getFname().length()<4){
this.addFieldError("fname", "first name can not be less than 5");
System.out.println("console: first name can not be less than 5 "+user.getFname());
}
}
这篇关于execAndWait拦截器不适用于验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!