【问题标题】:Struts2 - How can I call a method from Action-Bean1 if I call Action-Bean2?Struts2 - 如果我调用 Action-Bean2,我如何从 Action-Bean1 调用方法?
【发布时间】:2011-05-24 06:50:26
【问题描述】:

我做了一种叫做 ProfileSelector 的动作,它是通过一些 ajax 调用(通过使用 JQuery 库)加载的。

这是代码:

// BEAN
public class ProfileSelector extends ActionSupport implements ParameterAware {
    private String profilePage;
    private Map<String, String[]> parameters;

    @Override
    public String execute() throws Exception {
        profilePage=getParameterValue("profilePage");
        return profilePage;
    }

    public String getParameterValue(String param) {
        if (getParameters().get(param)==null) return "main";
        return ((String[])getParameters().get(param))[0];
    }

    public Map<String, String[]> getParameters() { return parameters; }
    public void setParameters(Map<String, String[]> maps) { this.parameters=maps; }

    public String getProfilePage() { return profilePage; }
    public void setProfilePage(String profilePage) { this.profilePage=profilePage; }
}

// STRUTS.XML
<action name="profile" class="model.ProfileSelector" >
    <result name="main">/profile/profile_main.jsp</result>
    <result name="edit">/profile/profile_edit.jsp</result>
    <result name="editConfirm">/profile/profile_edit.jsp</result>
    <result name="pm">/profile/profile_pm.jsp</result>
    <result name="articles">/profile/profile_articles.jsp</result>
</action>

// PAGE.JSP
<c:choose>
    <c:when test="${profilePage=='edit'}">
        <s:div>
            EDIT
            <s:url id="edit" action="profile.action"><s:param name="profilePage">editConfirm</s:param></s:url>
            <sj:submit href="%{edit}" targets="profileContent" value="Edit" />
        </s:div>
    </c:when>

    <c:when test="${profilePage=='editConfirm'}">
        // HERE I NEED TO LOAD A VALUE FROM ANOTHER BEAN-ACTION, not the profile one
    </c:when>
</c:choose>

看起来(在代码中)这里我需要从另一个 BEAN-ACTION 加载一个值,而不是配置文件一个:在这里我需要从另一个加载(例如)一个方法行动豆。 (例如&lt;s:property value="someMethod" /&gt;

我该怎么做?我认为 Struts 是不可能的,因为我一次只能调用 1 个动作。那么,拦截器?我需要更改应用程序的整个结构吗?

告诉我。干杯

【问题讨论】:

    标签: ajax model-view-controller jsp struts2 javabeans


    【解决方案1】:

    // 豆

    public class ListBookAction extends ActionSupport {
        BookService bookService;
        List<Book> bookList;
    
        public List<Book> getPostedBooks(){
            List<Book> bookList = new ArrayList<Book>();
            bookList = bookService.getUserPostedBooks();
            return bookList;
        }
    
        public String show(){
            bookList = getPostedBooks();
            return "list";
        }
        public List<Book> getBookList() {
            return bookList;
        }
        public void setBookService(BookService bookService) {
            this.bookService = bookService;
        }
    }
    

    //STRUTS.XML

            <action name="*ListBook" class="com.example.ListBookAction" method="{1}">
                <result name="list"  type="tiles-defs">listbook.listbook</result>
            </action>       
    

    //来自浏览器

    http://localhost:8888/showListBook.action
    

    P.S:如果您熟悉 Struts 1,它的工作方式类似于 DispatchAction。

    【讨论】:

      【解决方案2】:

      有些事情是不正确的,因为您永远不想为任何事情执行 2 个不同的操作。没有办法这样做。但是,您可能能够从 jsp 进行 ajax 调用并指向不同的操作并加载值。

      【讨论】:

      • 那是我在 Struts2 上不理解的:不是真正的 MVC。它只是一个 A(ction)VC... :)
      • 是的,Struts2 是 MVC。我建议您选择一本关于 Struts2 的好书,因为您有很多问题。 manning.com/dbrown
      • 据我所知,MODEL 的存在是为了将我的数据管理到服务器(例如,我曾见过 Bean 的 JSF 范围)。在 struts2 上,我需要手动管理我的数据(例如将对象放入会话)。所以与其他程序语言没有太大区别。像 PHP 一样。也许我错了:)(我希望如此..)
      • 不能在一次调用中调用两个动作是不正确的,但它确实不是一个好主意。它被称为动作链接(不推荐,通常被视为糟糕的设计),但是当您调用第一个动作时,它会将其所有值加载到值堆栈中,然后调用下一个动作并将其所有值添加到值堆栈中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多