我正在尝试创建一个简单的Spring项目。一旦我运行项目,它会显示index.jsp的内容,但是当我输入hello.jsp时,它会显示其内容,但是当我输入hello.htm时,它会显示"请求的资源不可用。"错误信息。
springapp-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/hello.htm" class="springapp.web.HelloController"/>
</beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
我更改了bean类的地址,但没有显示任何错误。
<bean name="/hello.htm" class="springapp.web.HelloController1"/>
添加viewResolver之后,问题已解决,但我有另一个问题作为单独的问题发布。如果您对地址有疑问,请参阅this question。
答案 0 :(得分:1)
在springapp-servlet.xml中添加此代码
<bean name="/hello.htm" class="springapp.web.HelloController1"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>