如何在现有的struts2项目中集成struts2 restful服务

时间:2012-12-21 17:00:18

标签: struts2 restful-url

在下面的文件中,我刚刚将过滤器类从filterDispatcher更改为StrutsPrepareAndExecuteFilter,以实现struts restful服务。否则我没有改变任何事情。

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>unsr</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml; classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Struts 2 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
        <init-param>
            <param-name>struts.devMode</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>struts.rest.defaultExtension</param-name>
            <param-value>xml</param-value>
        </init-param>   
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <listener>
        <listener-class>
            org.nea.individualprofile.web.struts.uniservs.ContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>application</param-name>
            <param-value>org.abc.web.AppResources</param-value>
        </init-param>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>validate</param-name>
            <param-value>true</param-value>
        </init-param>

        <load-on-startup>2</load-on-startup>
    </servlet>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <mime-mapping>
        <extension>css</extension>
        <mime-type>text/css</mime-type>
    </mime-mapping>

    <welcome-file-list>
        <welcome-file>Index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/NotFound.jsp</location>
    </error-page>
    <error-page>
        <error-code>400</error-code>
        <location>/NotFound.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/Error.jsp</location>
    </error-page>
</web-app>

在下面的文件中,我添加了struts restful sevice的所有常量标记。以上3包括在我已经在我现有项目中使用的顶部,该项目在过去1年中运作良好。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd ">
<struts>    
    <include file="struts-default.xml"/>
    <include file="struts-employee.xml"/>
    <include file="struts-test.xml"/>

    <constant name="struts.mapper.class" value="composite" />
    <constant name="struts.mapper.composite" value="rest,struts"/>  
    <constant name="struts.convention.package.locators" value="example"/>
    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>
 </struts>

以下是我为restful service所做的课程。其中Employee是我的POJO课程

package org.abc.individualprofile.web.rest.example;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.abc.ia.services.util.uniserv.UniServGrantInfo;
import org.abc.spring.services.interfaces.uniservs.UniservSearchInterface;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.ValidationAwareSupport;

public class TestController extends ValidationAwareSupport implements ModelDriven<Employee> {

    private UniServGrantInfo ugiObj = new UniServGrantInfo();
    private Collection< Employee > list;

    public HttpHeaders index(){
        restGrantInfoList = uniservService.showAllgrants("ITSMAXK", "ALL", "ALL", "ALL");
        return new DefaultHttpHeaders("index").disableCaching();
    }

    public Employee getModel() {        
        return (Employee) (list!= null ? list: ugiObj);
    }
}

我面临两个问题:

  1. 当我输入(localhost:7001 / unsr / test.json)URL
  2. 时,它没有按我的方法
  3. 我现有的工作申请表现在不能正常工作

0 个答案:

没有答案