编程语言
首页 > 编程语言> > java – Spring-Jersey:如何返回静态内容?

java – Spring-Jersey:如何返回静态内容?

作者:互联网

题 :

>如何公开我的css /,images /,js /和其他静态文件?
>如何在我的索引视图中为控制器(不是String方法)返回JSP页面?

问题 :

>在努力解决问题#1时,其他项目使用过滤器jersey.config.servlet.filter.staticContentRegex(如此处所示Stackoverflow Question)我无法在项目设置中找到正常工作的依赖项.
>在努力解决问题#2时,我试图引入依赖关系来使用Viewable.问题 – 传递依赖关系会对使用适当的Spring和Spring类的Web应用程序产生负面影响.泽西岛(雪球变成了一个模糊错误的兔子洞)

完成项目> Github Project

依赖性> Complete POM File

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>

Web.xml> Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.component.ResourceRegister</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

控制器> SpringController.java

@Path("/")
@Component
public class SpringController {

@GET
@Produces(MediaType.TEXT_HTML)
public ?? getIndex() {

    //How do I return my index page??
    return ??
 }
}

解决方法:

“How do I expose my css/, images/, js/ and other static files?”

jersey.config.servlet.filter.staticContentRegex属性应该可以使用,但是您需要为要提供的所有类型的文件指定所有可能的模式.更简单的方法是使用属性jersey.config.servlet.filter.forwardOn404,如this answer中所述.对于这两个选项都是如此,您需要将Jersey配置为过滤器,而不是servlet.所以你的web.xml可能看起来像

<filter>
    <filter-name>Jersey</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.component.ResourceRegister</param-value>
    </init-param>
    <!-- pass to next filter if Jersey/App returns 404 -->
    <init-param>
        <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

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

“How do I return a JSP page in my controller (not a String method) for my index view?”

首先你需要的是jsp mvc依赖

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-mvc-jsp</artifactId>
    <version>2.25</version>
</dependency>

然后,您需要注册该功能

public ResourceRegister () {
    ...
    register(JspMvcFeature.class);
}

然后,您需要为所有jsp页面指定模板基本路径.例如,我使用/ WEB-INF / jsp

public ResourceRegister () {
    ...
    property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
}

因此,对于这种情况,所有jsp文件都应位于/ WEB-INF / jsp目录中.

示例jsp和控制器

的index.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>JSP Page</title>
        <link href="css/styles.css" rel="stylesheet">
    </head>
    <body>
        <h1>${it.hello} ${it.world}</h1>
    </body>
</html>

调节器

@Path("/")
public class HomeController {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable index() {
        Map<String, String> model = new HashMap<>();
        model.put("hello", "Hello");
        model.put("world", "World");
        return new Viewable("/index", model);
    }
}

public ResourceRegister () {
    ...
    register(HomeController.class);
}

这里,Viewable中的/ index指向index.jsp(我们不需要.jsp扩展名). Jersey知道如何从我们上面设置的属性中查找文件.

也可以看看:

> Complete example from Jersey project
> MVC Templates documentation

标签:java,spring,jersey,jersey-2-0,static-content
来源: https://codeday.me/bug/20190713/1453643.html