2010-12-12 35 views
1

我做了一種叫做ProfileSelector的動作,加載了一些Ajax調用(通過使用JQuery庫)。Struts2 - 如果我調用Action-Bean2,如何從Action-Bean1調用方法?

這是代碼:

// 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的方法。 (例如<s:property value="someMethod" />

我該怎麼辦呢?我覺得是不可能的Struts的,因爲我可以。所以,攔截器?我需要改變應用程序的我的整個結構在時間打電話僅1行動?

設我知道,乾杯

回答

2

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

1

有些東西不正確,因爲你永遠不想爲任何事情做兩個不同的動作,但是沒有辦法這樣做,但是你可能會從jsp做出一個ajax調用並指向不同的動作並加載該值。

+0

這是我不明白在Struts2上:是不是一個真正的MVC。它只是一個A(ction)VC ... :) – markzzz 2010-12-12 16:47:49

+0

是的,Struts2是MVC。我建議你選擇一本關於Struts2的好書,因爲你有很多問題。 http://www.manning.com/dbrown/ – 2010-12-12 20:35:33

+0

據我所知,MODEL存在管理我的數據到服務器(例如,我見過Bean在JSF上的範圍)。在struts2我需要手動管理我的數據(如將對象放入會話中)。因此,與其他程序語言相比,它們沒有太大區別。像PHP一樣。也許我錯了:)(我希望如此..) – markzzz 2010-12-12 21:27:00