
Spring MVC的@CrossOrigin注解的使用
一 控制器
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="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" "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="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:p="www.springframework/schema/p" xmlns:MVC="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xsi:schemaLocation=" www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc.xsd www.springframework/schema/context 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>四 测试
👁️ 阅读量:0
© 版权声明:本文《Spring MVC的@CrossOrigin注解的使用》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686614472a305840.html。