【问题标题】:Populating of list after a redirect action in struts2在struts2中重定向操作后填充列表
【发布时间】:2015-07-17 05:48:38
【问题描述】:

我有一个index.jsp,它会在成功登录时重定向到otl_homepage.jsp。 当用户登录时,我需要在otl_homepage.jsp 中显示一个列表。 在我的struts.xml 中,如果我给type="chain" 或根本不输入任何类型,就会获取列表。但是如果我使用type="redirectAction",列表将返回空值。 我如何获得这份清单? 该列表在我的 java 操作类中维护。

请在此处找到struts.xml 和 java 类

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.enable.DynamicMethodInvocation"
    value="false" />
  <constant name="struts.devMode" value="false" />
  <constant name="struts.custom.i18n.resources" value="MessageResource" />
   <constant name="struts.convention.result.path" value="/"></constant>

 <package name="default" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="authentication"
                class="com.opentext.planning.interceptor.LoginInterceptor"></interceptor>
            <interceptor-stack name="authStack">
                <interceptor-ref name="authentication"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="authStack"></default-interceptor-ref>

       <!--  <global-results>
            <result name="login" type="redirect">/home.action</result>
        </global-results>

        <action name="home">
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result>/index.jsp</result>
        </action>-->


        <action name="login" class="com.opentext.planning.view.OTLAuthentication" >
            <result name="success" type="redirectAction">Otl_Homepage</result>
            <param name="otlUserList">${otlUserList}</param>
            <result name="login">/index.jsp</result>
            <result name="failure">/index.jsp</result>
            <result name="failure2">/index.jsp</result>
            </action>
            <action name="Otl_Homepage" class="com.opentext.planning.view.OTLAuthentication" method="home">
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">/Otl_Homepage.jsp</result>
             <result name="failure">/index.jsp</result>
            <result name="failure2">/index.jsp</result>
            </action>

Java 类:

public String execute(){
    System.out.println("inside execute");



    //System.out.println("user is" +user.getUser());
   // System.out.println("username is " +user.getUserName());
    //if("pankaj".equals(user.getUserName()) && "admin".equals(user.getPassword())){
        //System.out.println("inside if");

        if (j_username != null) {
        System.out.println("inside function");
    System.out.println("user name is "+j_username);
   // add userName to the session
    System.out.println("user name is 2"+j_username);
    sessionMap.put("loginId", j_username);
   System.out.println("dunno if the will print");
       // user.setUserName(user.getUserName());
      //  sessionMap.put("USER", j_username);
        status = otlAuthenticationController.loginAuthentication(j_username,j_password);
        if(status == "success")
        {
            this.otlUserList= otlAuthenticationController.obtainList();
            System.out.println("size is"+otlUserList.size());
            return "success";
        }

    }
        return status;
}

【问题讨论】:

  • 您已经完全浏览了this comment 中的所有观察结果,以了解您的其他答案。它们比你想象的更重要,不仅是为了让我们理解你的代码,尤其是为了让去做。您在做极其简单的事情时遇到了很大的麻烦,而这些麻烦的很大一部分的罪魁祸首是整套格式和命名问题。请解决这个问题,否则我和这里的许多其他人甚至会在没有阅读完您的问题的情况下略过您的问题:/

标签: java jsp struts2 valuestack


【解决方案1】:

重定向到另一个操作后,您需要在home 操作中再次填充列表。 redirectAction 结果类型重定向到另一个操作。另一个动作实例化动作类的一个新实例。在您返回 JSP 结果之前,应该初始化并填充此实例。在 JSP 中,从 ValueStack 检索的列表值具有一个动作实例,如果动作类中存在公共 getter 方法,则该动作实例返回一个列表。

public String home(){
    this.otlUserList= otlAuthenticationController.obtainList();
    System.out.println("size is"+otlUserList.size());
    return "success";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 2021-05-09
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多