Struts2 - 如果我调用 Action-Bean2,如何从 Action-Bean1 调用方法?

发布于 2024-10-07 09:28:36 字数 2193 浏览 4 评论 0原文

我做了一种名为 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加载一个值,而不是配置文件:在这里我需要加载(例如)一个方法来自另一个 Action-Bean。 (示例

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

请告诉我。

I making a sort of action called ProfileSelector, that is loaded trought some ajax call (by using JQuery library).

This is the code :

// 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>

Looks (in the code) at HERE I NEED TO LOAD A VALUE FROM ANOTHER BEAN-ACTION, not the profile one : is here that i need to load (for example) a method from another Action-Bean. (example <s:property value="someMethod" />

How can I do it? I think is impossible with Struts, because I can call only 1 action at time. So, Interceptors? I need to change my whole structure of the application?

Let me know. Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2024-10-14 09:28:36

// BEAN

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 类似。

// BEAN

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>       

//From browser

http://localhost:8888/showListBook.action

P.S: If you familiar with Struts 1, it work like DispatchAction.

没︽人懂的悲伤 2024-10-14 09:28:36

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

There is something incorrect as you never want to goto 2 different actions for anything. There is no way to do so. However, you might be able to make an ajax call from the jsp and point to different action and load the value out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文