获取404试图从Jersey 2渲染JSP [英] Getting 404 trying to render a JSP from Jersey 2

查看:187
本文介绍了获取404试图从Jersey 2渲染JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jersey 2和JSP创建一个Web应用程序的简单示例,并将其部署到Glassfish 4.但是,我一直在尝试访问我的唯一资源时遇到404错误。

首先,项目结构:



以下是重要的类和文件:

web.xml 我将Jersey设置为筛选器,而不是servlet,因为我阅读它需要以这种方式进行定义,以便使其能够与JSP一起工作。

 < web-app version =3.0xmlns =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的/网络app_3_0.xsd>

< display-name>原型创建的Web应用程序< / display-name>

< filter>
< filter-name>球衣< / filter-name>
< filter-class> org.glassfish.jersey.servlet.ServletContainer< / filter-class>
< init-param>
< param-name> jersey.config.server.provider.packages< / param-name>
< param-value> com.xxx.sample-jersey2< / param-value>
< / init-param>
< init-param>
< param-name> jersey.config.servlet.filter.forwardOn404< / param-name>
< param-value> true< /参数值>
< / init-param>
< / filter>

< filter-mapping>
< filter-name>球衣< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

<! - < welcome-file-list> - >
<! - < welcome-file> index.html< / welcome-file> - >
<! - < welcome-file> index.htm< / welcome-file> - >
<! - < welcome-file> movies.jsp< / welcome-file> - >
<! - < / welcome-file-list> - >

< / web-app>

movies.jsp 非常简单:

 < html> 
< body>
< h2>清单影片< / h2>
< / body>
< / html>

自定义的ResourceConfig(我甚至不确定需要)

  package com.xxx.jersey2.config; 

导入org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;

public class CustomResourceConfig extends ResourceConfig {
public CustomResourceConfig(){
packages(com.xxx);
寄存器(LoggingFilter.class);
寄存器(JspMvcFeature.class);
//它也可能是jersey.config.server.mvc.templateBasePath
属性(jersey.config.server.mvc.templateBasePath,/ WEB-INF / jsp);
// ...根据需要添加更多属性...
}
}

和我的电影控制器:

  package com.xxx.jersey2.web; 

import com.xxx.jersey2.model.Movie;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.glassfish.jersey.server.mvc.Viewable;

@Path(movies)
public class MoviesController {
@GET
@Path(/ list)
public Viewable getMovies() {
列表< Movie> movies = new ArrayList< Movie>();

movies.add(新电影(百万美元宝贝,希拉里斯旺克));
movies.add(新电影(玩具总动员,Buzz Light Year));
movies.add(新电影(饥饿游戏,珍妮弗劳伦斯));

返回新的可见(movies.jsp);




$ b如果我尝试访问 https:// localhost:8181 / sample-jersey2 / 呈现简单的 index.jsp (仅显示Hello World消息)。但是,试图访问 https:// localhost:8181 / sample-jersey2 / movies / list / 会给我一个404错误。



有关此任何想法将不胜感激。
最好的祝愿

解决方案

您的方向正确。请在web.xml中注册您的自定义资源配置。

 < init-param> 
< param-name> javax.ws.rs.Application< / param-name>
< param-value> com.xxx.jersey2.config.CustomResourceConfig< / param-value>
< / init-param>

你可以像你已经做的那样去除init参数jersey.config.server.provider.packages在你的资源配置。最后一件事将返回新的Viewable(movies.jsp)更改为新的Viewable(/ movies.jsp)。希望它能起作用。


I'm trying to make a simple example of a web application using Jersey 2 and JSP, and to deploy it on Glassfish 4. However, I keep getting 404 errors trying to access my only resource.

First, the project structure:

Here are the important classes and files:

web.xml I'm setting Jersey as a filter, instead of a servlet, because I read it needs to be defined that way in order to make it work with JSP.

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

  <display-name>Archetype Created Web Application</display-name>

  <filter>
    <filter-name>jersey</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.xxx.sample-jersey2</param-value>
    </init-param>
    <init-param>
      <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>

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

  <!--<welcome-file-list>-->
    <!--<welcome-file>index.html</welcome-file>-->
    <!--<welcome-file>index.htm</welcome-file>-->
    <!--<welcome-file>movies.jsp</welcome-file>-->
  <!--</welcome-file-list>-->

</web-app>

movies.jsp is extremely simple:

<html>
<body>
  <h2>Listing Movies</h2>
</body>
</html>

The custom ResourceConfig (which I'm not even sure I need)

package com.xxx.jersey2.config;

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;

public class CustomResourceConfig extends ResourceConfig {
    public CustomResourceConfig() {
        packages("com.xxx");
        register(LoggingFilter.class);
        register(JspMvcFeature.class);
        // it may be also jersey.config.server.mvc.templateBasePath
        property("jersey.config.server.mvc.templateBasePath", "/WEB-INF/jsp");
        // ... more properties as needed ...
    }
}

And my controller for the Movies:

package com.xxx.jersey2.web;

import com.xxx.jersey2.model.Movie;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.glassfish.jersey.server.mvc.Viewable;

@Path("movies")
public class MoviesController {
    @GET
    @Path("/list")
    public Viewable getMovies() {
        List<Movie> movies = new ArrayList<Movie>();

        movies.add(new Movie("Million Dollar Baby", "Hillary Swank"));
        movies.add(new Movie("Toy Story", "Buzz Light Year"));
        movies.add(new Movie("Hunger Games", "Jennifer Lawrence"));

        return new Viewable("movies.jsp");
    }
}

If I try to access https://localhost:8181/sample-jersey2/ the simple index.jsp is rendered (which only shows a Hello World message). However, trying to access https://localhost:8181/sample-jersey2/movies/list/ gives me a 404 error.

Any ideas on this would be greatly appreciated. Best regards

解决方案

You're in right direction. Please register your custom resource config in web.xml.

     <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.xxx.jersey2.config.CustomResourceConfig</param-value>
    </init-param>

You can remove the init param jersey.config.server.provider.packages as you're already doing that in your resource config. One last thing change the return new Viewable("movies.jsp") to new Viewable("/movies.jsp"). Hopefully it will work.

这篇关于获取404试图从Jersey 2渲染JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆