无法使用ModelDriven拦截器执行Struts 2程序

时间:2014-07-28 08:10:56

标签: java xml jsp struts2

我是Struts 2框架的新手。我已经制作了一个用于理解模型驱动拦截器的程序。但我无法执行它。以下是文件列表,最后有一个输出(错误)。

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>
    <%-- <jsp:useBean id="ent" class="pack.Entity" scope="session" /> --%>
    <s:form method="get" action="go">
        <s:textfield name="t1" label="Name"></s:textfield>
        <s:password name="p1" label="Password"></s:password>
        <s:submit value="accept"></s:submit>
    </s:form>
</body>
</html>

Entity.java

package actions_pack;

public class Entity {
private String t1;
private String p1;
public String getP1() {
    return p1;
}

public void setP1(String p1) {
    this.p1 = p1;
}

public Entity() {
    super();
    // TODO Auto-generated constructor stub
}

public String getT1() {
    return t1;
}

public void setT1(String t1) {
    this.t1 = t1;
}

}

GoAction.java

package actions_pack;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
import com.opensymphony.xwork2.util.ValueStack;
public class GoAction implements ModelDriven<Entity> {
    private Entity en;

    public Entity getEn() {
        return en;
    }
    public void setEn(Entity en) {
        this.en = en;
    }
    public String execute(){
        System.out.println("inside action");
        if(en.getT1().equalsIgnoreCase("nitin")){
            return "success";
        }
        else{
            return "failure";
        }
    }
    @Override
    public Entity getModel() {
        System.out.println("inside model driven....");
        en=new Entity();
        return en;
    }
}

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="dd">
            <result-types>
            <result-type name="dispatcher"
                class="org.apache.struts2.dispatcher.ServletDispatcherResult"
                default="true" />
        </result-types>
        <interceptors>
        <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
        <interceptor-stack name="myStack">
        <interceptor-ref name="modelDriven"></interceptor-ref>
        </interceptor-stack>
        </interceptors>
        <action name="go" class="actions_pack.GoAction">
        <interceptor-ref name="myStack"></interceptor-ref>
            <result name="success" type="dispatcher" >/one/welcome.jsp</result>
            <result name="failure" type="dispatcher">/one/error.jsp</result>
        </action>
    </package>
</struts>

welcome.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>
Welcome, <s:property value="t1"/>
</body>
</html>

网页输出(500错误):

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
    actions_pack.GoAction.execute(GoAction.java:24)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.

Apache Tomcat/7.0.34

struts.xml文件中,我不想使用/扩展struts-default包。虽然,当我在params中包含model driven拦截器条目和struts.xml拦截器时,问题就解决了。这背后的原因是什么?任何人都可以指导我吗?

1 个答案:

答案 0 :(得分:2)

您在操作类中有一个属性,需要在操作执行之前进行初始化。你可以通过很多方式做到这一点。您选择的方式依赖于在执行操作之前运行的modelDriven拦截器。它调用getModel()将其推到值堆栈之上。因此,您的实体属性正在初始化。如果删除此拦截器,则在执行操作时将获得null指针异常。如果您的实体是一个简单的POJO,您可以自己实例化,那么只需内联而不是getModel()

private Entity en = new Entity();     

@Override
public Entity getModel() {
    System.out.println("inside model driven....");
    return en;
}

下一部分是关于params拦截器。如果您使用top拦截器之前的modelDriven拦截器,它使用OGNL来填充作为模型的params对象。将其与您的操作一起使用可以初始化您在execute()中引用的一些属性。例如,应初始化t1