一 控制器
package org.fkit.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
@CrossOrigin(maxAge=3600)
@Controller
public class CrossOriginController{
// 测试@CrossOrigin注解
@CrossOrigin(origins="http://localhost:8080/VariableTest")
@GetMapping(value="/welcome")
public String welcome() {
System.out.println("处理跨域请求");
return "welcome";
}
}
二 视图
<%@ 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/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试@CrossOrigin注解</title>
</head>
<body>
恭喜您,测试跨域访问成功!
</body>
</html>
三 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:p="http://www.springframework/schema/p"
xmlns:mvc="http://www.springframework/schema/mvc"
xmlns:context="http://www.springframework/schema/context"
xsi:schemaLocation="
http://www.springframework/schema/beans
http://www.springframework/schema/beans/spring-beans.xsd
http://www.springframework/schema/mvc
http://www.springframework/schema/mvc/spring-mvc.xsd
http://www.springframework/schema/context
http://www.springframework/schema/context/spring-context.xsd">
<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.fkit.controller"/>
<!-- 默认装配方案 -->
<mvc:annotation-driven/>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 视图解析器 p:prefix属性表示前缀 p:suffix 表示后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
</beans>
四 测试
更多推荐
Spring MVC的@CrossOrigin注解的使用
发布评论