开发者

How to use Servlet in Struts2

How to use servlets t开发者_运维技巧ogether with Struts2?


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

More Information

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


There are three ways to resolve this problem:

  1. add constant tag in struts.xml

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

  2. add suffix in servlet configuration in web.xml

    <servlet-mapping>

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

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

    </servlet-mapping>

    Because in struts 2, it will only intercept all the request end with .action, if this request do not have any suffix, it will automatically add it. When we make our servlet url-pattern have a suffix, then struts 2 will not intercept it anymore.

  3. implement a user-defined filter


Servlets technology is more low level architectural layer than Struts2. Even more Struts2 is embedded to your project as a filter (that is part of servlet technology).

So to add one more servlet just add to web.xml registration:

<servlet>

    <servlet-name>MyServlet</servlet-name>
    <servlet-class>class.MyServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>


If you need multi mapping servlet you can using:

<constant name="struts.action.excludePattern" value="/Servletname1, /Servletname2" />

But in struts, you should not using servlet url because it not unity. You can use ajax:

 $.ajax({
            url : "nameAction.action?param="+id,
                   type : "post",
            data : {
                'id' : id

            },
            success : function(data) {
    //          $('#result').html(data);
            },
            error : function(jqXHR, textStatus, errorThrown) {
                $('#result').html("Error");
            }
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜