SSM静态资源配置出问题大神救救我

qq_34534598 2019-03-31 04:55:46
通过Controller跳转页面后,发现资源不能访问,而且路径有问题。

controller:

@Controller
@RequestMapping("/book")
public class BookController {

@Autowired
private BookService bookService;

@Autowired
private CategoryService categoryService;

@RequestMapping(value = "/bookProduct",method = RequestMethod.GET)
private String bookProduct(){
return "single-product";
}
}






我项目的路径是:









web的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>ChatRobot</display-name>
<description>ChatRobot_Alpha_0.0.1</description>

<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 匹配所有请求,此处也可以配置成 *.do 形式 -->
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

MVC的配置:

<?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: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-3.0.xsd">

<!-- 扫描web相关的bean -->
<context:component-scan base-package="com.fangxiaofeng.controller"/>

<!-- 开启SpringMVC注解模式 -->
<mvc:annotation-driven/>

<!-- 静态资源默认servlet配置 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/fonts/" mapping="/fonts/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:default-servlet-handler/>

<!-- 配置jsp 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".html"/>
</bean>

</beans>
...全文
582 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfei8 2020-08-27
  • 打赏
  • 举报
回复

<!--静态资源导出出问题-->
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
吴家声 2019-04-06
  • 打赏
  • 举报
回复
视图解析器prefix少了WEB-INF?
@jydServer 2019-04-04
  • 打赏
  • 举报
回复

// 返回登录页面
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path
+ "/";
return = basePath+"/abcd";
@jydServer 2019-04-04
  • 打赏
  • 举报
回复
跳转之后的返回页面使用相对路径加载静态文件了!
建议改成绝对路径!
getdate 2019-04-04
  • 打赏
  • 举报
回复
因为你 <url-pattern>/</url-pattern> 是这样的,所有即便是静态资源的请求,他们也默认是要请求一个controller,但是有没有对应的controller,所以会报错.
getdate 2019-04-04
  • 打赏
  • 举报
回复
<servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 匹配所有请求,此处也可以配置成 *.do 形式 --> <url-pattern>/</url-pattern> </servlet-mapping> 关键在这里,你这样配置了以后,其他资源是无法访问到的,你需要添加默认的<servlet-mapping>,或者你把 <url-pattern>/</url-pattern>改掉.
NOTBLL 2019-04-01
  • 打赏
  • 举报
回复
你请求URL中的resources哪里来的? 你可以试一下在<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>配置都加上 /resources前缀如:<mvc:resources location="/resources/images/" mapping="/resources/images/**"></mvc:resources>
勤勤勤能补拙 2019-04-01
  • 打赏
  • 举报
回复
#1回复 不小心按了ctrl+enter提交了,csdn还不能删除自己的回复,你看#2就好,如果我说错了请指正。
勤勤勤能补拙 2019-04-01
  • 打赏
  • 举报
回复
你的项目名称是bookshop. 以访问css文件为例子,css资源文件夹都放在webapp下面, 你也配置了<mvc:resources location="/css/" mapping="/css/**"></mvc:resources> 解释这段xml配置的含义: location="/css/" 意味你访问http://ip:端口/[项目名]/css/[....] 时,假如springmvc没找到controller的mapping, 就会根据mapping="/css/**" 映射到 [项目webapp下]/css/[.....] 下面的资源。 所以访问css文件资源,正确的访问路径应该是:http://localhost:8080/bookshop/css/custom.css。 你试试看能不能访问到。如果可以其他的文件的访问也类似。
勤勤勤能补拙 2019-04-01
  • 打赏
  • 举报
回复
你的项目名称是bookshop. 以访问css文件为例子,css资源文件夹都放在webapp下面, 你也配置了<mvc:resources location="/css/" mapping="/css/**"></mvc:resources> 解释这段xml配置的含义: location="/css/" 意味你访问http://ip:端口/[项目名]/css/ ..... 时,假如springmvc没找到controller的mapping,就会根据mapping=""映射到 所以访问css文件资源,访问路径应该是:http://localhost:8080/bookshop/css/custom.css。 你试试看能不能访问到。如果可以其他的文件

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧