差異處
這裏顯示兩個版本的差異處。
Both sides previous revision 前次修改 下次修改 | 前次修改 | ||
java:web:struts:html_jsp_filter [2013/06/15 22:58] tony |
java:web:struts:html_jsp_filter [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 53: | 行 53: | ||
} | } | ||
</code> | </code> | ||
- | ===== ===== | + | ==== 透過Interceptor ==== |
- | 友藏內心獨白: 下次會透過interceptor試看看! | + | === web.xml === |
+ | 將所有URL請求都交給struts的StrutsPrepareAndExecuteFilter。 | ||
+ | <code xml> | ||
+ | <filter> | ||
+ | <filter-name>struts2</filter-name> | ||
+ | <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> | ||
+ | </filter> | ||
+ | <filter-mapping> | ||
+ | <filter-name>struts2</filter-name> | ||
+ | <url-pattern>*</url-pattern> | ||
+ | </filter-mapping> | ||
+ | </code> | ||
+ | === strus.xml === | ||
+ | 最重要的兩點: | ||
+ | - 定義struts.action.extension的constant包含html,讓struts知道html也是屬於Action的一種。 | ||
+ | - 假設必須登入才能使用的頁面為clientInfo.html,我必須定義一個名為clientInfo的action。它的interceptor為basic-stack,其中包含我所實作的AuthorizationInterceptor。 | ||
+ | <code xml> | ||
+ | <struts> | ||
+ | <constant name="struts.action.extension" value="action,do,html" /> | ||
+ | <package name="default" extends="struts-default"> | ||
+ | <interceptors> | ||
+ | <interceptor name="loginValidator" | ||
+ | class="org.tonylin.funny.web.AuthorizationInterceptor" /> | ||
+ | <interceptor-stack name="basic-stack"> | ||
+ | <interceptor-ref name="loginValidator" /> | ||
+ | <interceptor-ref name="defaultStack" /> | ||
+ | </interceptor-stack> | ||
+ | </interceptors> | ||
+ | |||
+ | <global-results> | ||
+ | <result name="login">/login.html</result> | ||
+ | </global-results> | ||
+ | <action name="clientInfo"> | ||
+ | <interceptor-ref name="basic-stack" /> | ||
+ | </action> | ||
+ | </package> | ||
+ | </struts> | ||
+ | </code> | ||
+ | === AuthorizationInterceptor === | ||
+ | 檢查session是否包含使用者登入的資訊,如果沒有就導入登入頁面,如果有就照原本的流程繼續做下去。 | ||
+ | <code java> | ||
+ | public class AuthorizationInterceptor extends AbstractInterceptor { | ||
+ | private static final long serialVersionUID = -8706948299919053366L; | ||
+ | private Logger logger = LoggerFactory.getLogger(AuthorizationInterceptor.class); | ||
+ | |||
+ | @Override | ||
+ | public String intercept(ActionInvocation arg0) throws Exception { | ||
+ | logger.debug("Check app session."); | ||
+ | |||
+ | Map<String, Object> session = arg0.getInvocationContext().getSession(); | ||
+ | if( session.get(SessionKeys.ACCOUNT) == null ){ | ||
+ | return Action.LOGIN; | ||
+ | } | ||
+ | return arg0.invoke(); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | ===== Summary ===== | ||
+ | 兩種方法哪一個好呢? 我個人使用了第一種Filter的方式。因為使用Interceptor的方式,我必須將所有必須要登入才能使用的頁面,都宣告在struts設定檔中。我很懶所以使用了第一種方法。如果使用了第二種方法,為了避免讓html宣告與原本身為Controller的Action宣告容易造成閱讀上的不便,建議可以透過struts2的功能,將html部分的宣告另外拉出成一個設定檔,這部分可以參考Reference 3的做法。\\ | ||
+ | \\ | ||
+ | 友藏內心獨白: 做中學習,並藉以學習不同的方法。 | ||
===== Reference ===== | ===== Reference ===== | ||
* [[http://www.bestlong.idv.tw/thread-922-1-1.html|如何攔截JSP?]] | * [[http://www.bestlong.idv.tw/thread-922-1-1.html|如何攔截JSP?]] | ||
+ | * [[http://ceoajun.iteye.com/blog/1442479|Struts2中的constant]] | ||
+ | * [[http://www.mkyong.com/struts2/struts-2-include-multiple-struts-configuration-files/|如何引用多個設定檔]] | ||
===== ===== | ===== ===== | ||
---- | ---- | ||
\\ | \\ | ||
~~DISQUS~~ | ~~DISQUS~~ |