sta*_*low 3 java spring jersey static-content jersey-2.0
题 :
css/,images/,js/等静态文件吗?问题 :
jersey.config.servlet.filter.staticContentRegex(如此处所示)Stackoverflow问题我无法通过项目设置找到正常工作的依赖项.Viewable.问题 - 传递依赖关系会对使用适当的Spring和Jersey类别(将雪球变成模糊错误的兔子洞)对webapp产生负面影响完成项目 > Github项目
依赖 > 完整的POM文件
<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Controller > SpringController.java
@Path("/")
@Component
public class SpringController {
@GET
@Produces(MediaType.TEXT_HTML)
public ?? getIndex() {
//How do I return my index page??
return ??
}
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*tha 14
"我如何公开我的css /,images /,js /和其他静态文件?"
该jersey.config.servlet.filter.staticContentRegex属性应该可以使用,但是您需要为要提供的所有类型的文件指定所有可能的模式.更简单的方法是使用该属性jersey.config.servlet.filter.forwardOn404,如本答案中所指出的.对两种选择都是肯定的,你需要将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>
Run Code Online (Sandbox Code Playgroud)
"如何为我的索引视图返回控制器中的JSP页面(不是String方法)?"
首先你需要的是jsp mvc依赖
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-jsp</artifactId>
<version>2.25</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后,您需要注册该功能
public ResourceRegister () {
...
register(JspMvcFeature.class);
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要为所有jsp页面指定模板基本路径.比如我用的/WEB-INF/jsp
public ResourceRegister () {
...
property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
}
Run Code Online (Sandbox Code Playgroud)
因此,对于这种情况,所有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>
Run Code Online (Sandbox Code Playgroud)
调节器
@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);
}
Run Code Online (Sandbox Code Playgroud)
在这里,/index在Viewable点index.jsp(我们不需要的.jsp扩展名).Jersey知道如何从我们上面设置的属性中查找文件.
也可以看看:
| 归档时间: |
|
| 查看次数: |
7011 次 |
| 最近记录: |