今天继续开发自己的第一个博客系统。此役在配置过程中遇到了一些问题,目前为止还未完全解决。看来亲自动手做确实不易。
这些问题都是在Spring基础上无法加载静态资源造成的--页面上的jpg和css都没有生效。记得公司的项目中配置了一些default的servlet-mapping,以防止Dispathcer拦截掉了这些请求从而导致静态资源无法访问。我自己试了一下还是没能成功。另外在寻求解决此问题的过程中还遇到的一些问题以及其解决方案,均如下列出。
1.default映射的配置
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
2.试图通过如下配置
<mvc:resources mapping="/javascript/**" location="/static_resources/javascript/"/>
<mvc:resources mapping="/styles/**" location="/static_resources/css/"/>
<mvc:resources mapping="/images/**" location="/static_resources/images/"/>
去拦截这些请求。不过mvc的prefix需要在xml的头部配置,先前不太明白这个概念,就只在头部添加了一个链接:
http://www.springframework.org/schema/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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
首先得为mvc配置xmlns,接下来配置xsi,其中必然是一个解释文件对应一个解释目录。这样才不会报错了。否则会报错:Spring配置文件异常:White spaces are required between publicId and systemId
3.曾试图复制项目重新导入Eclipse,再配置tomcat进行启动。不过当时项目报错,说的事HttpServletRequest这个类无法解析。解决办法就是去tomcat目录的lib文件夹拷贝servlet-api到项目中就可以了。
4.目前网上常常能看到的三种办法都没能解决我目前的问题。不知是不是我使用的Spring4的原因。