28
Dec/110
Dec/110
App Engine + JSP + Jersey Viewable Example
I’m putting together a simple App Engine webapp that’s using Jersey as the controller and JSP for the view. Wrapping a Jersey Viewable in a Response, provides a clean way to do this, but I had a bit of trouble getting everything wired up properly. After a little googling I found a post over at Don Park’s Daily Habit that helped me get things right. Here’s a full working example for anyone who’s trying to do the same thing:
Required JARs
I’m using Jersey 1.11
- asm-3.1.jar
- jersey-core-1.11.jar
- jersey-server-1.11.jar
- jersey-servlet-1.11.jar
web.xml
<!--?xml version="1.0" encoding="utf-8"?--> JaxRsApplication com.sun.jersey.spi.container.servlet.ServletContainer javax.ws.rs.Application example.JaxRsApplication com.sun.jersey.config.property.JSPTemplatesBasePath /WEB-INF/jsp com.sun.jersey.config.property.WebPageContentRegex /(images|js|styles|(WEB-INF/jsp))/.* JaxRsApplication /*
JAX-RS Application
package example; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; public class JaxRsApplication extends Application { @Override public Set> getClasses() { Set> classes = new HashSet>(); classes.add(HomeResource.class); return classes; } }
Root Resource
package example; import java.util.HashMap; import java.util.Map; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import com.sun.jersey.api.view.Viewable; @Path("/") public class HomeResource { @GET public Response get() { Map model = new HashMap(); model.put("word", "World"); return Response.ok(new Viewable("/home", model)).build(); } }
/WEB-INF/jsp/home.jsp
<!--?xml version="1.0" encoding="UTF-8"?-->
Hello ${it.word}