Can not connect JAX-RS service to MVC template

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I'm attempting to make use of JAX-RS' (Jersey) MVC pattern. Attempts to reach http://localhost:8080/myproject/foos/test result in an error that reads:

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

http://localhost:8080/myproject/foos results in the same error.

What am I missing?

Resource:

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);      }   } 

Views:

WEB-INF / jsp / resources / FooResource

  • index.jsp
  • view.jsp

web.xml:

jerseycom.sun.jersey.spi.container.servlet.ServletContainercom.sun.jersey.config.property.WebPageContentRegex/(resources|images|js|styles|(WEB-INF/jsp))/.*jersey/*ServletAdaptorcom.sun.jersey.spi.container.servlet.ServletContainerSet the default, base template path to the WEB-INF folder.com.sun.jersey.config.property.JSPTemplatesBasePath/WEB-INF/jsp1ServletAdaptor/*             30         

回答1:

Made the following changes:

web.xml:

30welcome.jspjerseycom.sun.jersey.spi.container.servlet.ServletContainercom.sun.jersey.config.property.packagescontrollerscom.sun.jersey.config.property.WebPageContentRegex/((WEB-INF/views))/.*com.sun.jersey.config.property.JSPTemplatesBasePath/WEB-INF/views/com.sun.jersey.config.feature.Redirecttruecom.sun.jersey.config.feature.FilterForwardOn404truejersey/*

Resource:

@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);      }   } 

Views:

  • \welcome.jsp
  • \WEB-INF\views\foos\
    • index.jsp
    • view.jsp


回答2:

I had this same error running under Jetty 9. The application ran fine using mvn clean jetty:run but had this error when packaged as a war and deployed under Jetty. This is the fix in web.xml that worked for me:

         com.sun.jersey.config.property.JSPTemplatesBasePath -            /WEB-INF/views/ +            /WEB-INF/views

Yep, that's it. So, hopefully this helps someone who stumbles across this. My config is basically the same as craig's, but had the extra slash.



回答3:

From initial inspection I think you want to put index.jsp and view.jsp directly in WEB-INF/jsp.



回答4:

The name should be a fully qualified name like /index.jsp or /index.html.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!