Java execAndWait拦截器不使用验证
使用Java execAndWait拦截器不使用验证,java,validation,jsp,struts2,Java,Validation,Jsp,Struts2,使用execAndWait拦截器和validate()方法时,我得到了NullPointerException。在index.jsp中,我有一个textfield用于firstName。当我提交时,它首先使用validate方法在Action类中验证该字符串。然后转到execute方法。在输出中,我得到firstName字段的NullPointerException,该字段未在validate方法中找到。在某个地方我知道,execAndWait在不同的线程中运行,这就是为什么会出现这个问题。我想
execAndWait
拦截器和validate()
方法时,我得到了NullPointerException
。在index.jsp
中,我有一个textfield
用于firstName
。当我提交时,它首先使用validate方法在Action
类中验证该字符串。然后转到execute
方法。在输出中,我得到firstName
字段的NullPointerException
,该字段未在validate
方法中找到。在某个地方我知道,execAndWait
在不同的线程中运行,这就是为什么会出现这个问题。我想知道如何解决这个问题。文件类型代码如下:
index.jsp
:
在此处插入标题
struts.xml
:
/success.jsp
/failure.jsp
/index.jsp
/wait.jsp
GoAction.java
:
包装;
导入java.sql.*;
导入com.opensymphony.xwork2.ActionSupport;
导入com.opensymphony.xwork2.ModelDriven;
公共类GoAction扩展ActionSupport实现模型驱动{
私有静态最终长serialVersionUID=1L;
私人用户;
公共用户getUser(){
返回用户;
}
公共void setUser(用户){
this.user=用户;
}
@凌驾
公共用户getModel(){
System.out.println(“*****inside getModel()*****”);
user=新用户();
返回用户;
}
public void validate(){
System.out.println(“****内部验证****”);
如果(user.getFname().length()更改验证方法的逻辑
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());
}
}
public void validate(){
System.out.println(“****内部验证****”);
if(user.getFname()==null | | user.getFname().length()==0){
this.addFieldError(“fname”,“名为空”);
}否则
如果(user.getFname().length()解决方案在于execAndWait
拦截器的工作方式,以及struts2框架调用方法的方式(getModel()
,然后validate()
然后execute
)。还要注意,execAndWait
在不同的线程中运行。您不能使用ActionContext,因为它是ThreadLocal。这意味着如果您需要访问会话数据,则需要实现SessionAware,而不是调用ActionContext.getSession()
----execAndWait如何工作------
正如@Roman C和API文档中所述
execAndWait
在超时后返回等待结果,以反复调用您的操作,直到执行该操作。如果没有参数,则不会将其设置为该操作,并且验证总是失败
上面的程序正在抛出NullPointerException
,原因是execAndWait
拦截器每隔1秒重复调用getModel()
(以及validate()
)
因此,当它再次调用getModel()
时,它将设置原始新用户();
对象,即没有任何参数fname
。因此,在getModel()
之后,它进入validate()
再次。这次它发现fname
为null
。因此,要解决上述问题,只需将GoAction
类替换为以下代码:
目标行动
package pack;
import java.sql.*;
import java.util.Map;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class GoAction extends ActionSupport implements ModelDriven<User>,SessionAware {
private SessionMap<String, Object> sm;
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 u=(User) sm.get("user");
if(u==null){
user=new User();
}
else{
user=u;
}
return user;
}
public void validate(){
sm.put("user", user);
User u=(User) sm.get("user");
System.out.println("^^^"+u.getFname());
System.out.println("****inside validate****");
if(u.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());
}
}
public String execute() throws InterruptedException{
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;
}
@Override
public void setSession(Map<String, Object> sm) {
this.sm=(SessionMap<String, Object>) sm;
}
}
包装;
导入java.sql.*;
导入java.util.Map;
导入org.apache.struts2.dispatcher.SessionMap;
导入org.apache.struts2.interceptor.SessionAware;
导入com.opensymphony.xwork2.ActionSupport;
导入com.opensymphony.xwork2.ModelDriven;
公共类GoAction扩展了ActionSupport实现了模型驱动的SessionAware{
非公开会议地图;
私有静态最终长serialVersionUID=1L;
私人用户;
公共用户getUser(){
返回用户;
}
公共void setUser(用户){
this.user=用户;
}
@凌驾
公共用户getModel(){
System.out.println(“*****inside getModel()*****”);
用户u=(用户)sm.get(“用户”);
如果(u==null){
user=新用户();
}
否则{
用户=u;
}
返回用户;
}
public void validate(){
sm.put(“用户”,用户);
用户u=(用户)sm.get(“用户”);
System.out.println(“^^^”+u.getFname());
System.out.println(“****内部验证****”);
如果(u.getFname().length(),则表示“名为空”。即使它将值插入db。此外,如果我从struts.xml
文件中删除execAndWait
拦截器条目,那么验证方法的旧代码就没有问题。它是在值插入db之前还是之后说的?首先,很抱歉反应太晚。这是我得到的控制台输出ing:***内部getModel()************内部验证*********内部执行****2015年4月26日10:33:00 PM org.apache.struts2.util.TokenHelper警告:找不到映射到令牌名令牌的令牌确定****内部getModel()*********内部验证****您能告诉我…为什么要在getModel()
和validate()中输入
方法?如果我从struts.xml中删除execAndWait
interceptor条目,那么输出将显示:**inside getModel()********内部验证*********内部执行****
您应该了解拦截器的工作原理,它会在超时后返回一个等待
结果,反复调用您的操作,直到操作执行为止。如果没有参数,则不会将其设置为操作,并且验证始终失败。如果删除验证
拦截器
package pack;
import java.sql.*;
import java.util.Map;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class GoAction extends ActionSupport implements ModelDriven<User>,SessionAware {
private SessionMap<String, Object> sm;
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 u=(User) sm.get("user");
if(u==null){
user=new User();
}
else{
user=u;
}
return user;
}
public void validate(){
sm.put("user", user);
User u=(User) sm.get("user");
System.out.println("^^^"+u.getFname());
System.out.println("****inside validate****");
if(u.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());
}
}
public String execute() throws InterruptedException{
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;
}
@Override
public void setSession(Map<String, Object> sm) {
this.sm=(SessionMap<String, Object>) sm;
}
}