2012-08-22 14 views
0

我發現了一種情況,我想通過導入共享ActionBean的輸出在許多頁面上包含相同的內容。使用Stripes,我可以將ActionBean的輸出分辨率包含到jsp中嗎?

我想要做的是有一個ActionBean,它接受一些參數並執行一些處理,然後將一個ForwardResolution返回給JSP,該JSP使用標準Stripes構造如${actionBean.myValue來呈現該ActionBean的輸出。

然後我想從其他JSP「調用」這個ActionBean。這會將第一個ActionBean的輸出HTML放到第二個JSP的輸出中。

我該怎麼做?

回答

2

您可以通過使用<jsp:include>標籤獲得所需的結果。

SharedContentBean.java

@UrlBinding("/sharedContent") 
public class SharedContentBean implements ActionBean { 

    String contentParam; 

    @DefaultHandler 
    public Resolution view() { 
     return new ForwardResolution("/sharedContent.jsp"); 
    } 
} 

在你的JSP

<!-- Import Registration Form here --> 
<jsp:include page="/sharedContent"> 
    <jsp:param value="myValue" name="contentParam"/> 
</jsp:include> 

的web.xml

確保添加INCLUDE<filter-mapping>標籤在web.xml :

<filter-mapping> 
    <filter-name>StripesFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <servlet-name>StripesDispatcher</servlet-name> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>ERROR</dispatcher> 
</filter-mapping> 
+1

如果你發現自己經常這樣做,Stripes有非常強大的佈局機制。 http://www.stripesframework.org/display/stripes/Layout+Reuse – lucas

+0

謝謝。我們使用條紋布局標籤。在這種情況下,我們想要導入的組件不在一個一致的位置,而且我們也不希望它在_every_頁面上,所以我不認爲Stripes Layout結構會對這種情況有所幫助。可能我錯過了一些東西。 – JBCP

1

讓您希望包含相同內容的每個ActionBean擴展相同的BaseAction並將getter/setter放在那裏。例如:

BaseAction.class

package com.foo.bar; 

public class BaseAction implements ActionBean { 

    private ActionBeanContext context; 

    public ActionBeanContext getContext() { return context; } 
    public void setContext(ActionBeanContext context) { this.context = context; } 

    public String getSharedString() { 
    return "Hello World!"; 
    } 

} 

的index.jsp

<html> 
    <jsp:useBean id="blah" scope="page" class="com.foo.bar.BaseAction"/> 
    <body> 
    ${blah.sharedString} 
    </body> 
</html> 
+0

這太籠統了,在這種情況下,我希望HTML(jsp)也是java代碼的外部。 – JBCP

+0

更新了JSP以反映您的需求中的新變化。 –

+0

該解決方案不適合用於返回複雜信息,例如,getSharedString()應該能夠返回jsp的輸出。此解決方案也不是IoC,因爲可能會出現ActionBean需要多個BaseAction Bean的情況。對不起,我應該在我以前的評論中解釋過。 – JBCP

相關問題