SpringMVC及拦截器基本配置

导包就不记了。
web.xml:
	<!-- Spring MVC 核心控制器 通过此DispatcherServlet分发到各逻辑单元-->
	<servlet>
		<servlet-name>coreAction</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- <init-param>节点中配置了名为“contextConfigLocation”的
		Servlet参数,此参数指定了Spring配置文件的位置“/WEB-INF/Config.xml”。
		如果忽略此设定,则默认为“/WEB-INF/<servlet name>-servlet.xml”,其
		中<servlet name>以Servlet 名替换(在当前环境下,默认值也就是
		/WEB-INF/Dispatcher-servlet.xml)
		<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/Config.xml</param-value>
		</init-param>
	-->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<param-name>forceEncoding</param-name>
		<param-value>true</param-value>

	</filter>

	<servlet-mapping>
		<servlet-name>coreAction</servlet-name>
		<url-pattern>*.kara</url-pattern>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

接着是spring配置,由于是没在web.xml手动配置指定哪个spring配置文件,所以指向的是配置文件名是:coreAction-servlet.xml。
	<!-- 视图 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass">
			<value>org.springframework.web.servlet.view.JstlView</value>
		</property>
		
		<property name="prefix">
			<value>/WEB-INF/JSP/</value>
		</property>
		
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	
	<!-- Mapping -->
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/login.do">loginAction</prop>
				<prop key="toAddUser.kara">userAction</prop>
				<prop key="addUser.kara">userAction</prop>
			</props>
		</property>
		<!-- 注册拦截器 -->
		<property name="interceptors">
			<list>
			    <ref bean="myInterceptor"></ref>
			</list>
		</property>
	</bean>
	

	<!-- Controller -->
	<bean id="loginAction" class="com.biz.LoginAction"></bean>
	<bean id="userAction" class="com.biz.UserAction"></bean>
	<!-- 拦截器      -->
	<bean id="myInterceptor" class="com.intercept.MyInterceptor"></bean>

贴一个controller:
package com.biz;

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

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

/**
 * @author Administrator
 * 实现单个方法的controller
 */
public class LoginAction implements Controller  {

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setAttribute("kara", "Hello!Kara!");
		
		return new ModelAndView("hello");
	}
	 
}

贴上拦截器:
package com.intercept;

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


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyInterceptor  extends HandlerInterceptorAdapter{

	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("回调执行");
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("调用controller后进入");
	}
	
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		System.out.println("拦截开始:");
		System.out.println("***");
		System.out.println("&&&:"+arg2.getClass().getCanonicalName());
		System.out.println(arg2.getClass().getSimpleName());
		return false;
	}
	
}

你可能感兴趣的:(spring,Web,mvc,xml,servlet)