java web开发入门十二(idea创建maven SSM项目需要解决的问题)基于intellig idea(2019-11-09 11:23)
一、spring mvc action返回string带双引号问题
解决方法:
在springmvc.xml中添加字符串解析器
<!-- 注册string和json解析适配器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
二、css文件中引用的img url写法
暂未解决
三、action请求去掉.action
1.web.xml配置
<!-- 注册springmvc核心控制器-->
<servlet>
<!--servlet-name的值对应一个文件:/WEB-INF/DispatcherServlet-servlet.xml -->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定application.xml文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- <url-pattern>*.action</url-pattern>-->
<!--默认匹配所有请求-->
<!--Spring MVC将捕获Web容器所有的请求,包括静态资源的请求,Spring MVC会将它们当成一个普通请求处理-->
<url-pattern>/</url-pattern>
</servlet-mapping>
2.修改spring.xml文件,让静态文件交给servlet处理
参考:https://www.cnblogs.com/jdbn/p/11020374.html
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 -->
<!-- 简化配置:
(1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
-->
<mvc:annotation-driven/> <!--
静态资源交给servlet处理,
在springMVC-servlet.xml中配置<mvc:default-servlet-handler />后,
会在Spring MVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,
它会像一个检查员,对进入DispatcherServlet的URL进行筛查,
如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,
如果不是静态资源的请求,才由DispatcherServlet继续处理。
-->
<mvc:default-servlet-handler/>
四、登录拦截器
1.编写LoginInterceptor
package com.eggtwo.euq.interceptor; import com.eggtwo.euq.dto.CurrentSysUser;
import com.eggtwo.euq.utils.CacheUtil;
import com.eggtwo.euq.utils.ConfigUtil;
import com.eggtwo.euq.utils.CookieUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter; public class LoginInterceptor implements HandlerInterceptor {
private boolean isAjaxRequest(HttpServletRequest request) {
if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
//如果是ajax请求响应头会有,x-requested-with
System.out.print("发生ajax请求...");
return true; }
return false;
} @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String requestUri = request.getRequestURI(); //请求完整路径,可用于登陆后跳转
String contextPath = request.getContextPath(); //项目下完整路径
String url = requestUri.substring(contextPath.length()); //请求页面
System.out.print("发生拦截...");
System.out.println("来自:" + requestUri + "的请求"); //拿到cookie
//也就是获取session里的登录状态值
String cookieKey = ConfigUtil.getBossCookieKey();
String cookieValue = CookieUtil.getByName(request, cookieKey);
CurrentSysUser currentSysUser =null;
String errorMsg=null;
if (cookieValue == null) {
errorMsg="no login,please login";
}else{
currentSysUser = CacheUtil.getT(cookieValue);
if (currentSysUser == null) {
errorMsg="no login,please login";
}
}
if (errorMsg!=null){
if (isAjaxRequest(request)) {
response.setContentType("application/json; charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("{type:0,msg:'"+errorMsg+"'}");
writer.close();
response.flushBuffer();
} else {
String basePath= request.getContextPath();
response.sendRedirect(basePath+"/account/login?error="+errorMsg);
}
return false;//返回false不走下面的方法
}
//更新缓存过期时间
CacheUtil.remove(cookieValue);
CacheUtil.set(cookieValue, currentSysUser, ConfigUtil.getBossCookieTimeoutSecond());
//更新cookie过期时间--覆盖原有的cookie
CookieUtil.deleteCookie(response, cookieKey);
//写入cookie
CookieUtil.addCookie(response, cookieKey, cookieValue, ConfigUtil.getBossCookieTimeoutSecond()); return true;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}
2.spring mvc中配置要拦截的action
<!--配置拦截器, 多个拦截器,顺序执行 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 先匹配所有路径,然后排除不需要检查的路径 -->
<mvc:mapping path="/**"/> <!--不拦截的action-->
<mvc:exclude-mapping path="/account/login"/>
<mvc:exclude-mapping path="/account/logout"/> <!-- 网站的登录路径是 "http://localhost:8080/cultivate-job/"
路径path="/"表示的路径就是网站入口路径,
也就是说拦截器只方向两种请求:
1. 错误页面,直接访问jsp页面,这些页面不在WEB-INF目录下,可以直接访问
2. 网站入口请求,检查到没有登录,会重定向到网站入口路径,再被定向到登录页面
-->
<mvc:exclude-mapping path="/"/>
<!-- 以下是静态资源 -->
<mvc:exclude-mapping path="/content/**" />
<mvc:exclude-mapping path="/images/**" />
<mvc:exclude-mapping path="/js/**" />
<mvc:exclude-mapping path="/css/**" />
<mvc:exclude-mapping path="/upload/**" />
<mvc:exclude-mapping path="/download/**" /> <!-- 自定义拦截器路径 -->
<bean class="com.eggtwo.euq.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
<!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</mvc:interceptors>
五:异常拦截器
LoginInterceptor
java web开发入门十二(idea创建maven SSM项目需要解决的问题)基于intellig idea(2019-11-09 11:23)的更多相关文章
- java web开发入门十(idea创建maven SSM项目)基于intellig idea
一.搭建项目骨架 二.配置pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xm ...
- day04 Java Web 开发入门
day04 Java Web 开发入门 1. web 开发相关介绍 2. web 服务器 3. Tomcat服务器启动的问题 4. Tomcat目录结构 5. Web应用程序(虚拟目录映射,缺省web ...
- java web 开发入门实例
学习是个技巧活,关键是要找到重点的地方,新手在这方面的坑尤其多.看别人的教程一步一步的跟着做,隔几步就遇到一个新知识点,忍不住就百度往深处了解,一晃半天就过去了. 有的知识点要深入学习的,有的是了解下 ...
- java WEB开发入门
WEB开发入门 1 进入web JAVASE:标准- standard JAVA桌面程序 GUI SOCKET JAVAEE:企业-浏览器控制 web 2 软件结构 C/S :client ...
- java web 开发入门 --- tomcat/servlet/jsp
在做java web 开发时,要先安装tomcat.它是一个web服务器,也叫web容器,我们把写好的jsp, html页面放到它里面,然后启动它,就可以用浏览器访问这些页面,地址栏中输入localh ...
- java web 开发入门
Java web,是java技术用来解决web互联网领域的技术总和.Java web技术主要包括客户端和服务端,java在客户端的服务有java applet,不过用的非常少,大部分应用在服务端,比如 ...
- JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)
接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...
- java web开发入门二(struts)基于eclispe
JavaBean JavaBean, 咖啡豆. JavaBean是一种开发规范,可以说是一种技术. JavaBean就是一个普通的java类.只有符合以下规定才能称之为javabean: 1)必须提 ...
- java web开发入门九(Maven使用&idea创建maven项目)基于intellig idea
Maven 1.解决的问题 jar包的依赖和管理:版本.依赖关系等 自动构建项目 2.maven介绍 1.Maven是什么? Apache Maven是一个软件项目管理的综合工具.基于项目对象模型(P ...
随机推荐
- Spring Cloud Eureka 服务注册中心(二)
序言 Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件 它主要包括两个组件:Eureka Server 和 Eureka Client Eureka Clie ...
- JS读取xml
xml文件 <?xml version="1.0" encoding="utf-8"?> <root> <data id=&quo ...
- codeforces #592(Div.2)
codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...
- Vim操作:打开文件
1.打开文件并定位到某一行 vim +20 vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php # 定位至第20行 2 ...
- 最新版windows安装支持输入shell命令的工具cygwin教程
首先去官网下载自己对应系统32位或64位系统版本安装包:https://cygwin.com/install.html 下载好后按提示一步一步安装,直到这一步: 初次安装,这里是空的,没有下载的镜像链 ...
- iOS开发之--隐藏状态栏
1,全局隐藏 在Targets->General->勾选中Hide status bar .,如下图: 2.单个页面隐藏/展示状态栏 1).首先在info.plist里面View cont ...
- vue学习指南:第三篇(详细) - vue的生命周期
今天小编给大家详细讲解一下 vue 的生命周期.希望大家多多指教,哪里有遗漏的地方,也请大家指点出来 谢谢. 一. 怎么理解 Vue 的生命周期的? 生命周期:从无到有,到到无的一个过程.Vue的生命 ...
- 北京地铁出行线路规划系统项目总结(Java+Flask+Vue实现)
北京地铁出行线路规划系统项目总结 GitHub仓库地址:https://github.com/KeadinZhou/SE-Subway Demo地址:http://10.66.2.161:8080/ ...
- Scrum冲刺博客
一.各个成员在Alpha阶段认领的任务 已完成 二.各个成员的任务安排 三.整个项目期的任务量 按实际考试情况以及开发情况决定,初始计划是完成登录以及个人目标版块的完整功能,其它版块共进,保证最终能够 ...
- Axel多线程工具安装
Axel 是 Linux 下一个不错的轻量级高速下载工具,支持HTTP/FTP/HTTPS/FTPS协议,支持多线程下载.断点续传,且可以从多个地址或者从一个地址的多个连接来下载同一个文件. 大家使用 ...