前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分享一个SpringMVC的工具类

分享一个SpringMVC的工具类

作者头像
静谧星空TEL
发布2021-04-27 10:01:15
4550
发布2021-04-27 10:01:15
举报
文章被收录于专栏:云计算与大数据技术

利用Spring IOC机制编写HttpServletUtil工具

工具目的:

使用servlet的缺点:

  1. 一个servlet类只能写一个请求方法(当然可以通过判断参数实现不同的请求),代码冗余
  2. 只能在doGet、doPost或者service方法中编写

SpringMVC的不足之处:

  • 编写的SpringMVC可以使用Model,ModelAndView,ModelMap等对象进行传参,
  • 但是!!!使用ModelAndView方法的放回类型必须的ModelAndView,使用Model和ModelMap必须写在方法的参数中
  • 使用原生的servlet API request和session传参必须在方法的参数中声明才能使用。

因此,使用HttpServletUtil配合SpringMVC可以在SpringMVC类中通过注解注入工具类直接使用request对象、response对象、application对象和session对象,并且不用把这四个对象放在方法中声明便可以传参。

1、在springMVC的配置文件中添加具有HTTP协议的bean和开启springmvc的注解功能

代码语言:javascript
复制

2、编写工具类 HttpServletUtil.java

代码语言:javascript
复制
package com.gxwz.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HttpServletUtil {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	@Autowired
	private HttpSession session;
	@Autowired
	private ServletContext application;
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public HttpSession getSession() {
		//return request.getSession();
		return session;
	}
	public ServletContext getApplication() {
		return application;
	}

}

3、编写测试类 web.java

代码语言:javascript
复制
package com.gxwz.web;

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

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gxwz.util.HttpServletUtil;

@Controller
public class Web{
	
	@Autowired
	private HttpServletUtil httpServletUtil;
	
	@RequestMapping("/web")
	public String service() {
		
		List list = new ArrayList();
		list.add("公司大法官");
		list.add("国史大纲");
		list.add("根深蒂固");
		
		HttpServletRequest request = httpServletUtil.getRequest();
		HttpServletResponse response = httpServletUtil.getResponse();
		HttpSession session = httpServletUtil.getSession();
		ServletContext application = httpServletUtil.getApplication();
		
		System.out.println("request:"+request);
		System.out.println("response:"+response);
		System.out.println("application:"+application);
		System.out.println("sessionId:"+session.getId());
		
		request.setAttribute("list", list);
		session.setAttribute("list", list);
		application.setAttribute("list", list);
		
		return "web.jsp";
	}
}

4、编写跳转页面 web.jsp 测试效果

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>




Insert title here


	
	欢迎访问SpringMVC测试网页
	
	使用EL表达式获取: ${list }
	
	使用pageContext获取: ${pageContextScope.list }
	
	使用application获取: ${applicationScope.list }
		
	使用request获取: ${requestScope.list }
	
	使用session获取: ${sessionScope.list }

5、运行效果

总结:

这样就可以使用工具直接使用servlet的三大作用域的对象了,是不是很方便!!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 利用Spring IOC机制编写HttpServletUtil工具
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档