当前位置:首页 > 开发 > Web前端 > JavaScript > 正文

Jersey采用JSP模板

发表于: 2014-01-17   作者:cywhoyi   来源:转载   浏览次数:
摘要: Jersey作为Oracle的rest架构的开源框架,它既然是servlet,那么它也同样支持跟页面的交互,这里我采用了JSP作为模板来体现跟web page的交互,当然它也支持freemarker、velocity等模板引擎。   工程采用maven构建,第一步maven需要引入的lib包 <dependency> <groupId>org.g

Jersey作为Oracle的rest架构的开源框架,它既然是servlet,那么它也同样支持跟页面的交互,这里我采用了JSP作为模板来体现跟web page的交互,当然它也支持freemarker、velocity等模板引擎。

 

工程采用maven构建,第一步maven需要引入的lib包

<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet-core</artifactId>
			<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
			<!-- artifactId>jersey-container-servlet</artifactId -->
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.ext</groupId>
			<artifactId>jersey-mvc-jsp</artifactId>
		</dependency>
		<!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> 
			<artifactId>jersey-media-moxy</artifactId> </dependency> -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>xalan</groupId>
			<artifactId>xalan</artifactId>
			<version>2.7.1</version>
		</dependency>
		<dependency>
			<groupId>xerces</groupId>
			<artifactId>xercesImpl</artifactId>
			<version>2.11.0</version>
		</dependency>

 第二步:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container, 
	see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>vote</display-name>
<!-- 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	 -->
	<filter>
		<filter-name>AuthFilter</filter-name>
		<filter-class>org.ewell.filter.AuthFilter</filter-class>
	</filter>
	<filter>
		<filter-name>jersey</filter-name>
		<filter-class>com.agenda.base.AppServletContainer</filter-class>
		<init-param>
			<param-name>javax.ws.rs.Application</param-name>
			<param-value>com.agenda.AgendaApplication</param-value>
		</init-param>
		<init-param>
			<param-name>javax.servlet.Filter</param-name>
			<param-value>org.ewell.filter.AuthFilter</param-value>
		</init-param>
		<init-param>
			<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
			<param-value>/.*</param-value>
		</init-param>
		<init-param>
			<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
			<param-value>/</param-value>
		</init-param>
		<init-param>
			<param-name>com.sun.jersey.config.feature.Redirect</param-name>
			<param-value>true</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>
		<filter-name>jersey</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>AuthFilter</filter-name>
		<url-pattern>/rest/*</url-pattern>
	</filter-mapping>
</web-app>
 

 第三步:

ResourceConfig的配置

package com.agenda;

import org.ewell.filter.AuthFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
public class AgendaApplication extends ResourceConfig {

	public AgendaApplication() {
		property("jersey.config.server.mvc.templateBasepath.jsp", "");
		// Resources.
		packages(ContactosResource.class.getPackage().getName());
		// MVC.
		register(JspMvcFeature.class);
		
		// Logging.
		// register(LoggingFilter.class);
		
		
		//
		register(AuthFilter.class);
	}
}

 第四步:

独立的ServletContainer

package com.agenda.base;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ewell.model.basic.RequestThreadLocal;
import org.ewell.model.basic.RequestUtil;
import org.glassfish.jersey.servlet.ServletContainer;

public class AppServletContainer extends ServletContainer {
	private static final long serialVersionUID = -766501864493188353L;

	@Override
	public void doFilter(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		RequestThreadLocal reqThreadLocal = RequestUtil
				.bindRequest(httpRequest);
		String requestURI = httpRequest.getRequestURI();
		System.out.println("AppServletContainer" + requestURI);
		if ((requestURI.contains("login"))
				|| keepAliveUser(reqThreadLocal, requestURI)
				|| !(requestURI.contains("rest"))) {

			super.doFilter(request, response, chain);
		} else {
			HttpServletResponse httpResponse = (HttpServletResponse) response;
//			httpResponse.sendRedirect("/vote/index.html");
			super.doFilter(request, response, chain);
		}
	}

	private boolean keepAliveUser(RequestThreadLocal reqThreadLocal,
			String requestURI) {
		boolean isAlive = (requestURI.contains("rest"))
				&& (reqThreadLocal.getUser() != null);

		System.out.println("isAlive" + isAlive);
		return isAlive;
	}
}

 最后一步:

Resource的编写

package com.agenda.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.ewell.model.User;
import org.ewell.model.basic.RequestUtil;
import org.glassfish.jersey.server.mvc.Template;
import org.glassfish.jersey.server.mvc.Viewable;
import org.springframework.stereotype.Component;

import com.agenda.Contacto;
import com.agenda.ContactosRepository;
import com.agenda.base.BaseResource;

@Component
@Path("/rest/users")
public class UserResource extends BaseResource {

	@GET
	@Path("/login")
	@Template
	@Produces(MediaType.TEXT_HTML)
	public Viewable login() {
		User user = new User();
		RequestUtil.setOperator(getHttpRequest(), user);
		return new Viewable("/index", null);
	}

}

 至于index.jsp页面可以JSTL标记语言编写,跟其他servlet的框架一样

 

 

结论:

         jersey其实在中国国内并不多见,主要国内采纳的是springMVC或者struts2等框架,但是我个人觉得如果你想学习rest的风格,我强烈推荐jersey framework,spring或者struts2的注解都特别的复杂,很难把控住,jersey比较通俗易懂,缺点也很明显,就是相关的中文文档偏少,不过貌似ITEYE中有位博主,忘记姓名了;第二它自身存在的servlet通病,并发性较差;第三步相较于springMVC,它体现出来的MVC思想感觉没体现。

Jersey采用JSP模板

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
背景 最近刚入职新公司,浏览一下新公司项目,发现项目中大多数JSP页面都是独立的、完整的页面,因
这篇文章在myeclipse上成功.不过对于其他版本也是可以借鉴的.大不了也就是文件名字的版本号不同罢了
采用模板类实现的好处是,不用拘泥于特定的数据类型。就像活字印刷术,制定好模板,就可以批量印刷
同样是Maven构建的项目,不解释。 项目目录结构: pom.xml <project xmlns="http://maven.apache
所使用的myeclipse版本是 MyEclipse Enterprise Workbench Version: 10.0 Build id: 10.0-20111028 1
所使用的myeclipse版本是 MyEclipse Enterprise Workbench Version: 10.0 Build id: 10.0-20111028 1
在X:\Program Files\MyEclipse 6.0\myeclipse\eclipse\plugins\com.genuitec.eclipse.wizards_6.0.1.
这篇文章在myeclipse上成功.不过对于其他版本也是可以借鉴的.大不了也就是文件名字的版本号不同罢了
所使用的myeclipse版本是 MyEclipse Enterprise Workbench Version: 10.0 Build id: 10.0-20111028 1
所使用的myeclipse版本是 MyEclipse Enterprise Workbench Version: 10.0 Build id: 10.0-20111028 1
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号