一、Spring MVC项目无法引入js,css的问题
具体原因是css和js等被SpringMVC拦截了:
解决方案:在spring-mvc.xml中配置<mvc:default-servlet-handler/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-autowire="byName">
<!-- 视图解析器,用于访问/WEB-INF/jsp/路径下的JSP文件-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 定义支持注解的HanlderMapping,用于分发请求 -->
<mvc:annotation-driven/>
<!--静态资源处理,不加上的话js、css等会被SpringMVC拦截-->
<mvc:default-servlet-handler/>
</beans>
二、SpringMVC请求中RequestMapping映射了两个以上路径时,JS等静态资源无法访问
@requestMapping映射两个以上请求路径后,js和css的相对路径出问题了,导致跳转页面静态资源无法加载
通常,我们的html页面或jsp页面难免需要引入一些图片、js、css等文件,于是需要填写对应的文件路径,有相对路径和绝对路径两种写法,写相对路径时,如果通过controller后再返回对应的html或jsp页面时,相对路径可能会不是我们想要的,例如,在服务器端,如果某js文件路径为:/js/xx.js,html文件为/html/xx.html,并在在html中写入一下相对路径引用代码<script src="../js/jquery.min.js"></script>,会有两种结果发生,这里假设filter不拦截访问,并且配置了<mvc:resources location="/js/" mapping="/js/**" />,如果我们直接访问路径为:http://127.0.0.1:8080/(项目名)/html/xx.html,结果很一切正常,但是当我们通过controller映射返回对应的html页面时,可能就会出错了。
@Controller
@RequestMapping("/download")
public class DownloadController {
@RequestMapping(value="/develop/*",method=RequestMethod.GET,produces={"text/html"})
public String developPage() {
return "xx";
}
}
从上面的controller得出,我们访问xx.html的路径可写为:http://127.0.0.1:8080/(项目名)/download/develop/......
那么这个时候相对路径../js/xx.js在浏览器端解析后将为:http://127.0.0.1:8080/(项目名)/download/js/xx.js,于是悲催的事情就发生了,对应的js文件将得不到正常的引用,原因很简单,路径不对了,自然找不到,网上查找到的资料解释为:导入的js是相对当前请求的路径的,而不是相对于你在服务器文件的放置目录的。因此,正好印证了网上的解释,../js/xx.js被浏览器端解析后地址就是http://127.0.0.1:8080/(项目名)/download/js/xx.js。
那么怎么解决这个由controller引起的相对路径问题呢?
如果不经过controller映射,那么这个问题无需担心,大胆的写相对路径就是了,因为你的请求地址的目录结构和服务器的一致,如果要经过controller映射,方法如下:
假设是jsp页面,可以使用如下代码获取绝对路径地址:
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<script type="text/javascript" src="<%=basepath%>/js/xx.js"></script>
在JSP中<%!%>,<%%><%=%>三个标签有什么区别?
<%!%> 表示声明一个变量
<%%>表示所执行的jsp语句块,也就是相应的java代码
<%=%>相当于<%out.println("字符串");%>,在网页中显示某一个变量或某一个表达式的值
三、struts2项目引入SpringMVC
a、添加Maven依赖
<!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
b、修改web.xml,将struts2的拦截从/*改为拦截.do和.action并加入springmvc配置
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 引入SpringMVC配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.mvc</url-pattern>
</servlet-mapping>
<!-- End SpringMVC配置 -->
c、在src/main/resources中创建spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-autowire="byName">
<!-- 视图解析器 -->
<!-- 定义支持注解的HanlderMapping,用于分发请求 -->
<mvc:annotation-driven/>
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.ssit.whfcs.af.action.controller"/>
</beans>
d、创建controller测试
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController
{
@ResponseBody
@RequestMapping(value="/sayHello")
public String sayHello(){
System.out.println("hello");
return "hello";
}
@ResponseBody
@RequestMapping(value="/sayMessage")
public Message sayMessage(){
Message msg = new Message();
System.out.println("Message");
return msg;
}
}
e、访问
http://localhost:8090/whfcs/sayHello.mvc
返回结果:hello
五、SpringMVC中的重定向和转发的实现
5.1、请求转发和重定向的区别
请求重定向和请求转发都是web开发中资源跳转的方式:
a、请求转发是服务器内部的跳转
地址栏比发生变化
只有一个请求相应
可以通过request域对跳转目标的请求
b、请求重定向是浏览器自动发起对跳转目标的请求
地址栏会发生变化
两次请求相应
无法通过request域传递对象
5.2、SpringMVC中实现重定向和转发
(1)在SpringMVC中仍然可以使用传统方式实现转发和重定向
转发: request.getRequestDispatcher(" ").forward(request,response);
重定向:response.sendRedirect(" ");
(2)在SpringMVC中也提供了快捷方式实现转发和重定向
a、重定向
在返回时添加redirect:(页面地址/其他请求地址)
//重定向
@RequestMapping("/RedirectString")
public String RedirectString(User user)
{
System.out.println(user.getName());
if (user.getName()=="")
{
System.out.println("1");
return "redirect:login";
}
return "index";
}
b、请求转发
在返回时添加forward:(页面地址/其他请求地址)
//转发
@RequestMapping("/ForwardString")
public String forwordString(User user){
System.out.println(user.getName());
if (user.getName()==""){
System.out.println("1");
return "forward:login";
}
return "index";
}
(3)可以利用转发,实现允许用户访问WEB-INF下保存的指定资源
/**
* 通过转发 实现 访问到在WEB-INF目录下的资源
* @throws Exception
*/
@RequestMapping("/toFile")
public String toFile(String vname)
{
if("form".equals(vname)){
return vname;
}else{
return "err";
}
}
六、SpringMVC后台传递数据到页面(Model来传参和域对象传参)
SpringMVC后台传递参数到页面,Controller控制器中的参数传递到页面,常见的有两种方式:
方式一: 通过Model来传参(model对象来传递)
@Controller
@RequestMapping("mfc")
public class FirstController {
@RequestMapping(value="fr")
public String secondRequest(Model model){
String key = "hello world";
model.addAttribute("key", key);
//此时没有定义变量的名字,默认就用这个参数的类型的名字做为变量的名字,不过首字母大写变小写
model.addAttribute("xxxxxxx");
return "show";
}
}
model中有两个方法可以使用:model.addAttribute(object)和model.addAttribute("名字",object)。
传递后,在页面上通过EL表达式来获取,show页面代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h2>这里是show.jsp页面</h2>
通过model传递到页面的参数key:${key }<br/>
获取model单个参数存储的变量:${string }<br/>
</body>
</html>
方式二: 通过内置对象来传递
除了model传递参数以外,我们还可以通过request,session来传递,代码如下:
@Controller
@RequestMapping("mfc")
public class FirstController {
@RequestMapping(value="fr")
public String secondRequest(HttpServletRequest request,HttpSession session){
request.setAttribute("req", "通过request存放的参数");
session.setAttribute("ses", "session中的数据");
return "show";
}
}
页面上,还是通过EL表达式来获取,show页面内容如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h2>这里是show.jsp页面</h2>
获取request中的参数:${req }<br/>
获取session中的参数:${ses }
</body>
</html>
七、SpringMVC请求返回JSP页面
当返回值类型为String时,返回的是逻辑视图字符串也就是页面名字,由框架配置文件所配置的视图解析器拼接前后缀之后变成真正的物理视图,渲染出去。
//String返回值
@RequestMapping("/String")
public String ModelMap(String name)
{
System.out.println(name);
return "index";
}
八、Spring MVC通过CROS协议解决跨域问题
CORS简介:
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing)。它允许浏览器向跨源(协议 + 域名 + 端口)服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制。
CORS需要浏览器和服务器同时支持。它的通信过程,都是浏览器自动完成,不需要用户参与。对于开发者来说,CORS通信与同源的AJAX/Fetch通信没有差别,代码完全一样。浏览器一旦发现请求跨源,就会自动添加一些附加的头信息,有时还会多出一次附加的请求,但用户不会有感觉。
因此,实现CORS通信的关键是服务器。只要服务器实现了CORS接口,就可以跨源通信。
解决方案:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
可以参考:
九、Java中获取.properties配置文件中的参数值
avatar.path = /usr/local/apache-tomcat-8.5.40/webapps/avatar/
tempfile.path = /usr/local/apache-tomcat-8.5.40/webapps/tempfile/
要用到类java.util.ResourceBundle
ResourceBundle resource=ResourceBundle.getBundle("属性文件名不带扩展名");
//需要获取属性的时候调用
resource.getString(key);
原文地址:https://blog.csdn.net/CSDN2497242041/article/details/115211663
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。