Struts2 中添加 Servlet

Struts2中如何添加Servlet

之前Java開發都是Servlet的天下,現在是各類框架橫行,遇到一個須要將之前的Servlet加入到現有的Struts2的環境中。html

Google以後發現Stack Overflow真是個好東西,解決以下,只是簡單配置下。java

I assume you want to know how to use a servlet in conjunction with Struts2 when you have mapped everything to the Struts2 filter.

You can use the following in your struts.xml:

<constant name="struts.action.excludePattern" value="/YourServlet"/>
You can exclude multiple patterns by separating them with a comma, such as:

<constant name="struts.action.excludePattern" value="/YourServlet,/YourOtherServlet"/>
參考

Filter mapping for everthing to Struts2 besides one servlet?
Filters not working in Struts2web

Preventing Struts from Handling a Request

If there is a request that Struts is handling as an action, and you wish to make Struts ignore it, you can do so by specifying a comma separated list of regular expressions like:express

<constant name="struts.action.excludePattern" value="/some/conent/.*?" />
These regular expression will be evaluated against the request's URI (HttpServletRequest.getRequestURI()), and if any of them matches, then Struts will not handle the request.
To evaluate each pattern Pattern class from JDK will be used, you can find more about what kind of pattern you can use in the Pattern class JavaDoc.apache

除了上面在Struts2 裏面自帶的方法

1. 在web.xml中配置須要請求的Servlet

<servlet-mapping>

<servlet-name>Authcode</servlet-name>

<url-pattern>/authcode.servlet</url-pattern>

</servlet-mapping>

2. 在過濾器中Request的請求進行Servlet判斷並進行處理

參考api

相關文章
相關標籤/搜索