郁闷,原以为把html写好了,放到eclipse里就行了,结果问题一堆堆的。总结如下:
1.html调用不了,需要改成jsp才能访问
2.jsp显示乱码,调整eclpse默认编码,将html内容重新拷贝进来
3.jsp在eclipse中预览显示html,不是完整的样式结构,在浏览器中访问,访问不到样式文件,截图如下:
首先,确定html页面上引用的css,image,js资源是否正确。
朋友说,css,image,js不需要在WEB-INF下面,直接放在wepapp下面,更改后如下图。
资源位置:
显示效果:
注意路径中间多了个tjsite,正确的应该是http://localhost:8080/my_site/css/index.css.查看html页面,如下图:
需要控制页面的根路径为某固定路径下,添加basepath,添加后,页面正常显示,如下:
html页面如下:
页面效果如下:
过程中遇到奇怪的问题,我在网上搜js,css访问不到时,有人提供的方案里,有在spring-mvc.xml 中添加代码:
<mvc:default-servlet-handler />
<mvc:resources mapping="/styles/**" location="/css/"/>
如下图:
添加如上代码后,页面显示不出来了,报404错误,如下:
请教大神后发现:在spring-mvc.xml 添加 <mvc:annotation-driven /> 页面访问正常。spring-mvc.xml的配置如下:
<?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: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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="me.lyshi.mvc.controllers" />
<mvc:default-servlet-handler />
<mvc:resources mapping="/styles/**" location="/css/"/>
<!-- InternalResourceViewResolver jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp"
p:contentType="text/html; charset=utf-8"
/>
</beans>
现在,在html页面可以将css的访问路径从<link href="css/main.css" rel="stylesheet" type="text/css" />
改为<link href="styles/main.css"rel="stylesheet" type="text/css" />,此时页面又加载不上这个main.cs了,截图如下:
查找原因:需要把web.xml里将如下代码删掉,页面显示正常。
ok,问题解决,可以也用style/main.css访问,也可以用css/main.css访问。