Jersey中的基于MVC模式的JSP跳转方式:Viewable与request转发。
来源:互联网 发布:淘宝打开店铺自动收藏 编辑:程序博客网 时间:2024/05/16 17:00
由于是基于maven的项目,所以基础的spring与jersey包都可以从mavenrepository网站得到:http://mvnrepository.com/
除此之外,要想实现基于JSP模板的MVC模式jersey结构,当然还需要jersey-mvc-x.jar 和jersey-mvc-jsp-x.jar。都可以在mavenrepository中得到。
目的:通过一个小demo,来实现viewable或者request请求转发实现jersey的rest请求到JSP页面的跳转过程的说明。
项目结构:
A:创建jersey资源映射配置类ResourceConfig
public class RestApplication extends ResourceConfig { public RestApplication() { //服务类所在的包路径 packages("com.xiansky.action"); //注册JSON转换器 register(JacksonJsonProvider.class); register(JspMvcFeature.class); //注册MVC支持 property(JspMvcFeature.TEMPLATE_BASE_PATH, "/");//jsp文件所在位置,当前JSP文件是在项目根目录下 } }
B:配置jersey相关的web.xml
<web-app> <display-name>Archetype Created Web Application</display-name> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param><servlet><servlet-name>jerseyServlet1</servlet-name><servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class><init-param><param-name>javax.ws.rs.Application</param-name><param-value>com.xiansky.action.RestApplication</param-value></init-param> <init-param> <param-name>com.sun.jersey.config.feature.Redirect</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>jersey.config.servlet.filter.forwardOn404</param-name> <param-value>true</param-value> </init-param><!-- <load-on-startup>1</load-on-startup> --></servlet><servlet-mapping><servlet-name>jerseyServlet1</servlet-name><url-pattern>/jsp/*</url-pattern></servlet-mapping></web-app>
@Controller("viewJsp")@Path("/view")public class ViewJsp { Log log = LogFactory.getLog(ViewJsp.class); //通过request请求转发方式从rest风格的请求跳转到jsp页面-传递request域参数 @GET @Path("/dispatcher") public void renderToJSP(@Context HttpServletRequest request,@Context HttpServletResponse response){ log.info("request dispatcher...to jsp"); try { request.setAttribute("demo", "I LOVE LIUZHOU"); //请求转发 request.getRequestDispatcher("/demo.jsp").forward(request, response); } catch (Exception e) { log.error(e+"request dispatcher fail"); } } //通过viewable进行基于MVC模式的页面跳转,并可以封装参数到request域或者到viewable构造器第二个参数object(model)中 @GET @Path("/template/{city}") @Produces(MediaType.TEXT_HTML) public Viewable loveCityName(@PathParam("city")String cityName,@Context HttpServletRequest request){ log.info("use viewable tech render to jsp page >>>>>>>"+ cityName); //可以将属性设置到request域中,跳转后的jsp页面亦可从request域中取出 request.setAttribute("demo", "TOJSP"); //jersey viewable进行Jsp页面跳转 return new Viewable("/index",cityName); } }
D:跳转到的Jsp页面,获取参数并显示
demo.jsp ->request转发的页面。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <% String info = String.valueOf(request.getAttribute("demo")); %> <span><%=info %></span></body></html>
index.jsp -->viewable跳转到的jsp页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <% String city = String.valueOf(request.getAttribute("model")); //此处注意通过new Viewable("..",object);request只能通过指定'model'属性获取值 String info = String.valueOf(request.getAttribute("demo")); %> <span>I LOVE <%=city %>, <%=info %></span></body></html>E:测试:
总结:
重点是在A中的recourseConfig中对JSPMVC一些基础类进行注册和jsp基路径进行配置,以至于在后面通过viewable跳转jsp页面时候可以查找得到。还有通过viewable跳转到jsp中,在jsp中通过getAttribute方法只能获取指定的“model”属性,才能得到viewable传递过来的object内容。
该工程源代码:http://pan.baidu.com/s/1c2t7lg0
0 0
- Jersey中的基于MVC模式的JSP跳转方式:Viewable与request转发。
- JSP跳转方式与Servlet跳转方式的区别
- JSP页面跳转与请求转发
- JSP页面跳转与请求转发
- jsp跳转的方式
- JSP跳转的方式
- 请求的跳转与转发
- javaWeb_14-request实现请求转发和mvc设计模式
- request实现请求转发和mvc设计模式
- JSP页面跳转的两种方式----重定向和转发
- 浅谈Jsp页面的两种跳转方式:客户端重定向和服务器端转发
- JSP 跳转和转发
- jsp中的转发与重定向的区别
- Servlet和Jsp中的Request转发、重定向
- jsp页面的两种跳转方式(request和response的区别)
- jsp中的重定向(不同的跳转方式)
- JSP中页面的跳转方式respond.sentRedirect和request.getRequestDispatcher总结
- Spring MVC的跳转方式
- hdu5779 Tower Defence
- 51Nod-1051-最大子矩阵和
- MAVEN常用命令
- linux_基础命令
- Spark 源码调试一路通
- Jersey中的基于MVC模式的JSP跳转方式:Viewable与request转发。
- LeetCode进阶之路( Search for a Range)
- 加权异构信息网络中的基于语义路径的个性化推荐
- 初识PopupWindow
- hdoj 5765 Bonds
- Java IO 对字符的读取和写出
- Python 之 re模块 正则表达式
- 如何设置linux添加开机启动项
- 001:Why is it faster to process a sorted array than an unsorted array?