在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进行学习,记录下学习的过程