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

标签 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

  • 网页.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>
    

    最佳答案

    进行了以下更改:

    网页.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\views\foos\
  • index.jsp
  • view.jsp
  • 关于model-view-controller - 无法将 JAX-RS 服务连接到 MVC 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8978200/

    相关文章:

    java - 如何在 JAX-RS 异常上使用自定义消息设置 40X 错误?

    php - 在 Zend 中创建表单 View

    java - 使用 Jersey 查询参数验证

    java - com.owlike.genson.JsonBindingException : Could not deserialize to type class com. sun.jersey.api.representation.Form

    java - Jax RS 客户端调用微服务 -> javax.ws.rs.ProcessingException : wrong number of arguments

    java - Dropwizard:如何为 GET/PUT 添加自定义验证

    php - 关于构建自己的(PHP)MVC框架的问题

    java - java中的MVC模式,观察者中不止一种更新方法

    hibernate - 将 ORM 与严格的 MVC 框架结合使用

    spring - Jersey REST API 作为一个单独的网络应用程序