无法将JAX-RS服务连接到MVC模板

时间:2012-01-23 20:36:58

标签: model-view-controller jersey jax-rs

我正在尝试使用JAX-RS'(Jersey)MVC模式。尝试到达http://localhost:8080/myproject/foos/test会导致错误:

java.io.IOException: The template name, /view, could not be resolved to a fully qualified template name

http://localhost:8080/myproject/foos会导致同样的错误。

我错过了什么?

资源:

package resources;

import com.sun.jersey.api.view.Viewable;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("foos")
public class FooResource {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable get() {

        return new Viewable("/index", this);

    }   

    @GET
    @Path("{id}")
    @Produces(MediaType.TEXT_HTML)
    public Viewable get(@PathParam("id") String id) {

        return new Viewable("/view", id);

    } 

}

查看:

WEB-INF / jsp / resources / FooResource

  • 的index.jsp
  • view.jsp的

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/(resources|images|js|styles|(WEB-INF/jsp))/.*</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>jersey</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <description>Set the default, base template path to the WEB-INF folder.</description>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/jsp</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

</web-app>

3 个答案:

答案 0 :(得分:5)

进行了以下更改:

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>welcome.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>controllers</param-value>
        </init-param>        
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/((WEB-INF/views))/.*</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/views/</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.Redirect</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
            <param-value>true</param-value>
        </init-param>        
    </filter>
    <filter-mapping>
        <filter-name>jersey</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

资源:

@Path("foos")
public class FooResource {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable index() {

        return new Viewable("/foos/index", this);

    }   

    @GET
    @Path("{id}")
    @Produces(MediaType.TEXT_HTML)
    public Viewable view(@PathParam("id") String id) {

        return new Viewable("/foos/view", id);

    } 

}

查看:

  • \的welcome.jsp
  • \ WEB-INF \视图\ FOOS \
    • 的index.jsp
    • view.jsp的

答案 1 :(得分:1)

我在Jetty 9下运行了同样的错误。应用程序使用 mvn clean jetty运行但在打包为战争并在Jetty下部署时出现此错误。这是web.xml中的修复程序,对我有用:

         <init-param>
             <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
-            <param-value>/WEB-INF/views/</param-value>
+            <param-value>/WEB-INF/views</param-value>
         </init-param>
是的,就是这样。所以,希望这可以帮助那些偶然发现这一点的人。我的配置与craig的配置基本相同,但有额外的斜线。

答案 2 :(得分:0)

从初始检查开始,我认为您希望将index.jsp和view.jsp直接放在WEB-INF / jsp中。