Share a utility class SpringMVC

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq262593421/article/details/100528617

                           Spring IOC mechanism to use tools to write HttpServletUtil

 

Tool Purpose:

The disadvantages of using the servlet:

  1. A servlet class only a write request method (of course, may be implemented by determining different parameters of the request), code redundancy
  2. You can only write in doGet, doPost method or service

SpringMVC deficiencies:

  • Written SpringMVC can use Model, ModelAndView, ModelMap and other objects of mass participation,
  • but! ! ! ModelAndView method of use must be returned to the type of ModelAndView, use Model and ModelMap must be written in the parameter method
  • Use native servlet API request and the session must be declared in order to use mass participation in the parameter method.

Therefore, HttpServletUtil with SpringMVC tools can be injected directly through the request object annotation SpringMVC class, response objects, application objects and session objects, and without these four objects in the method statement can pass parameters.

 

1, add bean has the HTTP protocol and open springmvc comment function springMVC profile

    <!-- 1. 扫描具有HTTP协议的注解bean -->
    <context:component-scan base-package="com.gxwz.web" />

    <!-- 2. 开启springmvc的注解功能 -->
    <mvc:annotation-driven />

2, writing tools HttpServletUtil.java

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, write test classes web.java

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<String> list = new ArrayList<String>();
		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, write the page jump web.jsp test results

<%@ 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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!--  -->
	<h2>欢迎访问SpringMVC测试网页</h2>
	<hr>
	<b>使用EL表达式获取:</b> ${list }
	<hr>
	<b>使用pageContext获取:</b> ${pageContextScope.list }
	<hr>
	<b>使用application获取:</b> ${applicationScope.list }
	<hr>	
	<b>使用request获取:</b> ${requestScope.list }
	<hr>
	<b>使用session获取:</b> ${sessionScope.list }
	<hr>
	
</body>
</html>

5, operating results

 

to sum up:

This tool can be used directly target the three major scope of the servlet is not very convenient! ! !

 

 

 

Guess you like

Origin blog.csdn.net/qq262593421/article/details/100528617