关闭

SpringMVC 入门 -- Handler编写

标签: springmvc
240人阅读 评论(0) 收藏 举报
分类:

本文仅作代码的记录

结构:

libaraies:


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVCTest</display-name>
  <!-- Spring 前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- contextConfigLocation配置springMVC加载的配置文件(配置处理器、映射器、适配器等) -->
  	<!-- 默认加载/WEB-INF/SERVLET名称-servlet.xml(springmvc-servlet.xml) -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<!-- 第一种 *.action 访问以action结尾的 -->
  	<!-- 第二种 / 所有地址都解析。所以要配置静态资源不进行解析。使用此中方法可以实现RESTful风格 -->
  	<!-- 第三种 /* 这样配置不对。使用这种配置最终要转发到jsp页面时,仍然会由DispatcherServlet解析jsp地址,不能根据jsp找到handler,会报错 -->
  	
  	<url-pattern>*.action</url-pattern>
  	
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  	<filter-name>myFilter</filter-name>
  	<filter-class>com.yrsoft.filter.MyFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>myFilter</filter-name>
  	<servlet-name>springmvc</servlet-name>
  </filter-mapping>
</web-app>


springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
					http://www.springframework.org/schema/context 
					http://www.springframework.org/schema/context/spring-context-3.0.xsd
					http://www.springframework.org/schema/mvc
					http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
					http://www.springframework.org/schema/aop
					http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
					http://www.springframework.org/schema/tx
					http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
					">
					<!--  配置handler-->
					<bean name="/queryuser.action" class= "com.yrsoft.controller.ItemsController1"></bean>
					
					
					<!--  处理器适配器
						所有的处理器适配器都实现HandlerAdapter接口
					-->
					<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
					<!--  处理器映射器
						将bean的name当作url进行查找。需要在配置Handler时指定beanname(就是url)
					-->
					<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
					<!--  视图解析器
						解析jsp视图,默认使用jstl路径
					-->
					<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"/>
					
</beans>


controller

package com.yrsoft.controller;

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

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.yrsoft.po.Users;

/**
 * 实现Cintroller接口的处理器,一定要实现Controller接口
 *
 */
public class ItemsController1 implements Controller{
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//调用service查找数据。查询用户列表。
		
		List<Users> userList = new ArrayList<Users>();
		//填充静态数据
		Users user1 = new Users("admin","123");
		Users user2 = new Users("123", "345");
		userList.add(user1);
		userList.add(user2);
		
		//返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		//相当于request的setAttribute,在jsp页面中通过userList后去数据
		modelAndView.addObject("userList",userList);
		
		//指定视图
		modelAndView.setViewName("/WEB-INF/jsp/users/userList.jsp");
		
		
		return modelAndView;
	}

}


MyFilter

package com.yrsoft.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.yrsoft.po.Users;
/**一定要实现Filter接口*/
public class MyFilter implements Filter {
	@Override
	public void destroy() {
		System.out.println("Destroy ");
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest servletRequest = (HttpServletRequest) request;
		HttpSession session = servletRequest.getSession();
		Users user = (Users) session.getAttribute("user");
		if (user == null ) {
			System.out.println("注入User ");
			user = new Users("张三","123");
			session.setAttribute("user", user);
			request.getRequestDispatcher("/WEB-INF/jsp/users/userList.jsp").forward(request, response);
		} else {
			System.out.println(user.toString());
			chain.doFilter(request, response);
		}
//		chain.doFilter(request, response);
	}
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("init");
	}

}


chain.doFilter() 会再次进入这个filter执行doFilter,还没研究原因。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:38544次
    • 积分:678
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:10篇
    • 译文:0篇
    • 评论:2条
    最新评论