无法使用ModelDriven拦截器执行Struts 2程序 [英] Unable to execute Struts 2 program with ModelDriven interceptor

查看:45
本文介绍了无法使用ModelDriven拦截器执行Struts 2程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Struts 2 framework. I have made a program for understanding modelDriven interceptor. But I am unable to execute it. Following are the list of files and in the end there is a output (error).

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 包.虽然,当我在 struts.xml 中包含 params 拦截器条目以及 modelDriven 拦截器时,问题仍然可以解决.这背后的原因是什么?有人可以引导我吗?

In struts.xml file I don't want to use/extend struts-default package. Although, when I include params interceptor entry along with modelDriven interceptor in struts.xml, problem solves. What is the reason behind this. Can any one guide me?

推荐答案

操作类中有一个属性,该属性需要在执行操作之前进行初始化.您可以通过多种方式来做到这一点.

You have a property in the action class that needs initialize prior to the action execution. You can do it in many ways.

您选择的方式取决于 modelDriven 拦截器(在您执行操作之前正在运行).它调用 getModel()将其推入 valueStack top 上.因此,您的实体属性正在初始化.如果删除此拦截器,则在执行操作时将得到 NullPointerException .

The way you have chosen relies on modelDriven interceptor which is running before your action is executed. It invokes getModel() to push it on top of the valueStack. So, your entity property is being initialized. If you remove this interceptor you will get NullPointerException when the action is executed.

如果您的 Entity 是一个可以实例化自己的简单POJO,则只需内联而不是在 getModel()中进行.

If your Entity is a simple POJO that you can instantiate yourself then simply do it inline instead of in getModel().

private Entity en = new Entity();     

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

下一部分是关于 params 拦截器.如果您已使用top 对象.rel ="nofollow noreferrer"> modelDriven 拦截器之前的

Next part is about params interceptor. It uses OGNL to populate a top object that is a model if you have used modelDriven interceptor prior params interceptor.

将其与您的操作一起使用可以初始化您在 execute()中引用的某些属性.

Living it along with your action allows to initialize some properties you reference in the execute().

例如 t1 应该被初始化.

这篇关于无法使用ModelDriven拦截器执行Struts 2程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆