java - Spring-Jersey : How to return static content? -
question :
- how expose
css/
,images/
,js/
, other static files? - how return jsp page in controller (not string method) index view?
problems :
- in efforts solve question #1 other projects use filter
jersey.config.servlet.filter.staticcontentregex
(as seen here stackoverflow question) haven't been able find dependencies work correctly/at project setup. - in efforts solve question #2 attempted introduce dependencies use
viewable
. problem - transitive dependencies adversely affect webapp using appropriate classes spring & jersey (snowballs rabbit hole of nebulous errors)
complete project > github project
dependencies > 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>
controller > springcontroller.java
@path("/") @component public class springcontroller { @get @produces(mediatype.text_html) public ?? getindex() { //how return index page?? return ?? } }
"how expose css/, images/, js/ , other static files?"
the jersey.config.servlet.filter.staticcontentregex
property should work, need specify possible patterns types of files want serve. easier way use property jersey.config.servlet.filter.forwardon404
, pointed out in this answer. , yes both choices, need configure jersey filter, rather servlet. web.xml might
<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 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 return jsp page in controller (not string method) index view?"
first thing need jsp mvc dependency
<dependency> <groupid>org.glassfish.jersey.ext</groupid> <artifactid>jersey-mvc-jsp</artifactid> <version>2.25</version> </dependency>
then need register feature
public resourceregister () { ... register(jspmvcfeature.class); }
then need specify template base path jsp pages. example used /web-inf/jsp
public resourceregister () { ... property(jspmvcfeature.template_base_path, "/web-inf/jsp"); }
so case, jsp files should located in /web-inf/jsp
directory.
example jsp , controller
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>
controller
@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); }
here /index
in viewable
points index.jsp
(we don't need .jsp
extension). jersey knows how find file property set above.
see also:
Comments
Post a Comment