开发者

Struts2 - How can I call a method from Action-Bean1 if I call Action-Bean2?

开发者 https://www.devze.com 2023-01-29 23:32 出处:网络
I making a sort of action called ProfileSelector, that is loaded trought some ajax call (by using JQuery library).

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">/p开发者_JAVA百科rofile/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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消