Struts2 中添加 Servlet

本文介绍在Struts2框架中如何配置并使用Servlet。通过在struts.xml中指定excludePattern,可以避免Struts2处理特定的请求。此外,还可以在web.xml中直接配置Servlet映射,或在过滤器中进行请求判断。

Struts2中如何添加Servlet

以前Java开发都是Servlet的天下,如今是各种框架横行,遇到一个需要将以前的Servlet加入到现有的Struts2的环境中。

Google之后发现Stack Overflow真是个好东西,解决如下,只是简单配置下。

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 Struts2

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:

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

除了上面在Struts2 里面自带的方法

1. 在web.xml中配置需要请求的Servlet
<servlet-mapping>

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

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

</servlet-mapping>
2. 在过滤器中Request的请求进行Servlet判断并进行处理

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值