SpringMVC请求处理流程

在JSP页面中的href=http://192.168.0.112:8080/MAP/test/login' 发起http请求

如能响应请求,首先在web.xml中增加对springMVC框架的支持,增加SpringMVC框架核心过滤器的servlet的配置,在web.xml中添加如下配置:

	<servlet>
	    <!--为Servlet命名-->
		<servlet-name>springMVC</servlet-name>
		<!--这表示这个org.springframework.web.servlet.DispatcherServlet的servlet已经得到了注册名springMVC-->
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 利 用init-param元素向servlet提供初始化参数,init-param元素具有param-name和param-value子元素。-->
		<init-param>
		    <!--方法中调用getServletConfig(). getInitParameter("contextConfigLocation")获得"spring-mvc.xml"-->
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/spring-mvc.xml</param-value>
		</init-param>
		<!--当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet-->
		<load-on-startup>1</load-on-startup>
	</servlet>

在spring-mvc.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-lazy-init="true">
	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.map.controller" />
	<!-- 开启注解 -->
	<mvc:annotation-driven>
		<!-- 解决@ResponseBody乱码 -->
			<bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
				<property name = "supportedMediaTypes">  
                     <list>  
                         <value>text/plain;charset=UTF-8</value>  
                     </list>  
                </property>  
			</bean>  
		</mvc:message-converters> 
	</mvc:annotation-driven>
	<!-- 静态资源(js/image)的访问 -->
	<mvc:resources location="/js/" mapping="/js/**" />
	<mvc:resources location="/css/" mapping="/css/**" />
	<mvc:resources location="/images/" mapping="/images/**" />
	<mvc:resources location="/ckeditor/" mapping="/ckeditor/**" />
	<mvc:resources location="/upload/" mapping="/upload/**" />
	<mvc:resources location="/ueditor/" mapping="/ueditor/**" />
	<mvc:resources location="/appcache/" mapping="/appcache/**" />
	
	<!-- 拦截器注册 -->
	<mvc:interceptors>
        <bean id="DefaultInterceptor" class="com.map.common.interceptor.DefaultInterceptor"></bean> 	
        <bean id="RightInterceptor" class="com.map.common.interceptor.RightInterceptor">
         	<!-- 以下不做拦截处理 -->
         	<property name="excludedUrls">
	             <list>
	             	 <value>.*/MainFlow/.*</value>
	             	 <value>.*/FlowActions/.*</value>
	             	 <value>.*/briefManage/.*</value>
	             </list>
         	</property>
        </bean> 
	</mvc:interceptors>
	<!-- 拦截器注册 -->
	<!-- 定义视图解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 系统日志的配置 -->
	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="defaultErrorView">
			<value>failure</value>
		</property>
		<property name="exceptionMappings">
			<props>
				<prop key="java.sql.SQLException">showDBError</prop>
				<prop key="java.lang.RuntimeException">showError</prop>
			</props>
		</property>
		<property name="warnLogCategory" value="WARN"></property>
		<property name="defaultStatusCode" value="500"></property>
	</bean>
</beans>
根据spring-mvc.xml中的配置,http请求发送后,会扫描包com.map.controller包,在包下的user类中根据@RequestMapping("login"),匹配http的login请求

	@RequestMapping("login")
	@ResponseBody
	public String toLogin(String loginId,String password, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		User user= userService.getUserByLoginId(loginId);
		if(user!=null)
		{
			if(password!=null&&password.equals(user.getPassword()))
			{
				//更新用户登录信息-ip,time
				userService.updateLoginInfo(request.getRemoteAddr(), new Date(), user.getId());
				
				//添加用户Session信息
				UserSession us = new UserSession(session);
				us.setCurrentUser(user);
				return "success";
			}
			else{
				return "error";
			}
		}
		throw new BusinessException("错误操作!");
	}
在ajax中根据返回的结果,进行判读和跳转

			                error: function(request) {
			                    alert_dialog("连接错误!");
			                },
			                //请求成功后调用的回调函数,result由服务器返回,并根据dataType参数进行处理后的数据
			                success: function(result){
			                	//console.log(result);
								if(result=='success'){
									location.href='${cxt}/rbac/tologin';
								}else{
									$("label[for='user_password']").show();
								}
			                }

正在对springmvc进行学习,记录下学习的过程


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值