<i id='1WLDj'><tr id='1WLDj'><dt id='1WLDj'><q id='1WLDj'><span id='1WLDj'><b id='1WLDj'><form id='1WLDj'><ins id='1WLDj'></ins><ul id='1WLDj'></ul><sub id='1WLDj'></sub></form><legend id='1WLDj'></legend><bdo id='1WLDj'><pre id='1WLDj'><center id='1WLDj'></center></pre></bdo></b><th id='1WLDj'></th></span></q></dt></tr></i><div id='1WLDj'><tfoot id='1WLDj'></tfoot><dl id='1WLDj'><fieldset id='1WLDj'></fieldset></dl></div>

<legend id='1WLDj'><style id='1WLDj'><dir id='1WLDj'><q id='1WLDj'></q></dir></style></legend>
    1. <small id='1WLDj'></small><noframes id='1WLDj'>

    2. <tfoot id='1WLDj'></tfoot>

        • <bdo id='1WLDj'></bdo><ul id='1WLDj'></ul>

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

        Unable to execute Struts 2 program with ModelDriven interceptor(无法使用 ModelDriven 拦截器执行 Struts 2 程序)
          <tbody id='fedEX'></tbody>
          <i id='fedEX'><tr id='fedEX'><dt id='fedEX'><q id='fedEX'><span id='fedEX'><b id='fedEX'><form id='fedEX'><ins id='fedEX'></ins><ul id='fedEX'></ul><sub id='fedEX'></sub></form><legend id='fedEX'></legend><bdo id='fedEX'><pre id='fedEX'><center id='fedEX'></center></pre></bdo></b><th id='fedEX'></th></span></q></dt></tr></i><div id='fedEX'><tfoot id='fedEX'></tfoot><dl id='fedEX'><fieldset id='fedEX'></fieldset></dl></div>
          <tfoot id='fedEX'></tfoot>

            <small id='fedEX'></small><noframes id='fedEX'>

            • <bdo id='fedEX'></bdo><ul id='fedEX'></ul>

                1. <legend id='fedEX'><style id='fedEX'><dir id='fedEX'><q id='fedEX'></q></dir></style></legend>
                2. 本文介绍了无法使用 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() 将其推送到 valueStacktop 上.因此,您的实体属性正在初始化.如果您删除此拦截器,您将在执行操作时得到 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 拦截器 之前的 params拦截器.

                  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 应该被初始化.

                  For example t1 should be initialized.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                3. <small id='JGH98'></small><noframes id='JGH98'>

                    • <bdo id='JGH98'></bdo><ul id='JGH98'></ul>

                      <tfoot id='JGH98'></tfoot>
                        <tbody id='JGH98'></tbody>

                      • <legend id='JGH98'><style id='JGH98'><dir id='JGH98'><q id='JGH98'></q></dir></style></legend>
                        1. <i id='JGH98'><tr id='JGH98'><dt id='JGH98'><q id='JGH98'><span id='JGH98'><b id='JGH98'><form id='JGH98'><ins id='JGH98'></ins><ul id='JGH98'></ul><sub id='JGH98'></sub></form><legend id='JGH98'></legend><bdo id='JGH98'><pre id='JGH98'><center id='JGH98'></center></pre></bdo></b><th id='JGH98'></th></span></q></dt></tr></i><div id='JGH98'><tfoot id='JGH98'></tfoot><dl id='JGH98'><fieldset id='JGH98'></fieldset></dl></div>