users@jersey.java.net

Re: Jersey Guice JSP how to?

From: Zhanming Qi <qizhanming_at_gmail.com>
Date: Thu, 28 Oct 2010 09:11:54 -0700 (PDT)

Hi, Paul

Here is my code:

Jersey Module

new JerseyServletModule() {

        @Override
        protected void configureServlets() {
                // bind resources
                this.bind(IndexAction.class);

                // params.put(ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX,
"/style/.*");
                // this serves the static content
                this.serveRegex("/(images|style)/.*").with(DefaultWrapperServlet.class);
                                
                Map<String, String> params = new HashMap<String, String>();
                params.put(ResourceConfig.FEATURE_IMPLICIT_VIEWABLES, "true");
                params.put(ResourceConfig.FEATURE_REDIRECT, "true");
                params.put(ResourceConfig.FEATURE_TRACE, "true");
                params.put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, "true");

                this.filter("/*").through(GuiceContainer.class, params);
        }
});

IndexAction.java

@Path("/")
@RequestScoped
public class IndexAction {
        
    @QueryParam("x") String x;

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable doGetAsTextHtml(){
            x = "abc";
            return new Viewable("/index.jsp", this);
    }

    public String getX() {
        return x;
    }
}

DefaultWrapperServlet.java

@Singleton
public class DefaultWrapperServlet extends HttpServlet {

        public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
                RequestDispatcher dispatcher =
getServletContext().getNamedDispatcher("default");

                HttpServletRequest wrapped = new HttpServletRequestWrapper(request) {
                        public String getServletPath() {
                                return "";
                        }
                };
                dispatcher.forward(wrapped, response);
        }

}

pom.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
                <artifactId>enginx</artifactId>
                <groupId>com.example.en</groupId>
                <version>0.0.1-SNAPSHOT</version>
        </parent>
        <groupId>com.example.en</groupId>
        <artifactId>en-frontend</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <properties>
                <project.contextPath>/en</project.contextPath>

                <http.port>8080</http.port>
                <https.port>8443</https.port>
                <org.eclipse.jetty.version>8.0.0.M1</org.eclipse.jetty.version>

                <org.apache.derby.version>10.6.1.0</org.apache.derby.version>
                <com.sun.jersey.version>1.4</com.sun.jersey.version>
                <ch.qos.logback.version>0.9.24</ch.qos.logback.version>
                <com.google.inject.version>3.0-SNAPSHOT</com.google.inject.version>
        
<com.google.inject.servlet.version>3.0-SNAPSHOT</com.google.inject.servlet.version>
        </properties>
        <dependencies>
                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>${org.junit.version}</version>
                        <scope>test</scope>
                </dependency>
                <dependency>
                        <groupId>org.apache.derby</groupId>
                        <artifactId>derbyclient</artifactId>
                        <version>${org.apache.derby.version}</version>
                        <type>jar</type>
                </dependency>
                <!--jersey-test-framework-grizzly
           
com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory
           
com.sun.jersey.test.framework.spi.container.grizzly.GrizzlyTestContainerFactory
          -->
                <dependency>
                        <groupId>com.sun.jersey.jersey-test-framework</groupId>
                        <artifactId>jersey-test-framework-grizzly</artifactId>
                        <version>${com.sun.jersey.version}</version>
                        <scope>test</scope>
                </dependency>
        <!--jersey-test-framework-http
           
com.sun.jersey.test.framework.spi.container.http.HTTPContainerFactory
          -->
                <dependency>
                        <groupId>com.sun.jersey.jersey-test-framework</groupId>
                        <artifactId>jersey-test-framework-http</artifactId>
                        <version>${com.sun.jersey.version}</version>
                        <scope>test</scope>
                </dependency>
        <!--
          jersey-test-framework-inmemory
         
com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory
          -->
                <dependency>
                        <groupId>com.sun.jersey.jersey-test-framework</groupId>
                        <artifactId>jersey-test-framework-inmemory</artifactId>
                        <version>${com.sun.jersey.version}</version>
                        <scope>test</scope>
                </dependency>
        <!--jersey-test-framework-embedded-glassfish
           
com.sun.jersey.test.framework.spi.container.embedded.glassfish.TestContainerFactory
          -->
        <!--
        <dependency>
            <groupId>com.sun.jersey.jersey-test-framework</groupId>
           
<artifactId>jersey-test-framework-embedded-glassfish</artifactId>
            <version>${com.sun.jersey.version}</version>
            <scope>test</scope>
        </dependency>
          -->
        <!--jersey-test-framework-external
           
com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory
          -->
                <dependency>
                        <groupId>com.sun.jersey.jersey-test-framework</groupId>
                        <artifactId>jersey-test-framework-external</artifactId>
                        <version>${com.sun.jersey.version}</version>
                        <scope>test</scope>
                </dependency>
                <dependency>
                        <groupId>org.glassfish.web</groupId>
                        <artifactId>jstl-impl</artifactId>
                        <version>${javax.servlet.jsp.jstl.version}</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>com.google.inject</groupId>
                        <artifactId>guice</artifactId>
                        <version>${com.google.inject.version}</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>com.google.inject.extensions</groupId>
                        <artifactId>guice-persist</artifactId>
                        <version>${com.google.inject.version}</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>com.google.inject.extensions</groupId>
                        <artifactId>guice-servlet</artifactId>
                        <version>${com.google.inject.version}</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>com.sun.jersey</groupId>
                        <artifactId>jersey-server</artifactId>
                        <version>${com.sun.jersey.version}</version>
                        <type>pom</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>com.sun.jersey.contribs</groupId>
                        <artifactId>jersey-guice</artifactId>
                        <version>${com.sun.jersey.version}</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                        <version>1.6.1</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-core</artifactId>
                        <version>${ch.qos.logback.version}</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
                <dependency>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-classic</artifactId>
                        <version>${ch.qos.logback.version}</version>
                        <type>jar</type>
                        <scope>compile</scope>
                </dependency>
        </dependencies>
        <build>
                <finalName>en-frontend</finalName>
                <plugins>
                        <plugin>
                                <groupId>org.mortbay.jetty</groupId>
                                <artifactId>jetty-maven-plugin</artifactId>
                                <version>${org.eclipse.jetty.version}</version>
                                <configuration>
                                        <systemProperties>
                                                <systemProperty>
                                                        <name>logback.configurationFile</name>
                                                        <value>./src/main/resources/logback.xml</value>
                                                </systemProperty>
                                        </systemProperties>
                                        <connectors>
                                                <connector
implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                                                        <port>${http.port}</port>
                                                        <maxIdleTime>60000</maxIdleTime>
                                                </connector>
                                        </connectors>
                                        <scanIntervalSeconds>10</scanIntervalSeconds>
                                        <webAppConfig>
                                                <contextPath>${project.contextPath}</contextPath>
                                        </webAppConfig>
                                        <stopPort>9966</stopPort>
                                        <stopKey>stop</stopKey>
                                        <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
                                                <filename>./target/yyyy_mm_dd.request.log</filename>
                                                <retainDays>90</retainDays>
                                                <append>true</append>
                                                <extended>true</extended>
                                                <logTimeZone>GMT</logTimeZone>
                                        </requestLog>
                                </configuration>
                        </plugin>
                </plugins>
        </build>
</project>

some css in style folder, but got this:

HTTP ERROR 404

Problem accessing /en/style/reset.css. Reason:

    Not Found

Thank you for your help.
-- 
View this message in context: http://jersey.576304.n2.nabble.com/Jersey-Guice-JSP-how-to-tp5349718p5683239.html
Sent from the Jersey mailing list archive at Nabble.com.