javaee springMVC 放行静态资源
  V8pJJzS5Z7xm 2023年11月02日 100 0


spring配置文件

在配置文件中添加配置
<mvc:resources mapping=“/static/**” location=“/static/” />

<?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:p="http://www.springframework.org/schema/p"
       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">
      <!-- 1. 配置  需要扫描的控制层在哪个包  -->
    <context:component-scan base-package="com.test"></context:component-scan>

    <!-- 2 配置 视图解析器 中的 前缀和后缀  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 设置前缀  -->
        <property name="prefix" value="/WEB-INF/"/>
        <!-- 设置后缀 -->

        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 开启注解驱动
      注册了两个数据转换的注解@NumberFormatannotation支持,@DateTimeFormat
      json相关的。。
    -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 放行静态资源
        location:静态资源的真实路径
        mapping:访问路径(可以自定义)
        放行static文件夹下的所有的文件
     -->
    <mvc:resources mapping="/static/**" location="/static/" />


</beans>

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<img src="static/images/timg.jpg" />
<h2>Hello World!</h2>
<c:if test="${loginUser==null}">
    请<a href="users/loginUi">登录</a>
</c:if>
<c:if test="${loginUser!=null}">
    ${loginUser.uname},欢迎您!
</c:if>
</body>
</html>

结构

javaee springMVC 放行静态资源_spring


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
V8pJJzS5Z7xm