Spring简单例子开发 求大牛解答!
我按照例子在eclipse下写了一个很简单的spring页面,可是为毛tomcat一直显示404呢。
ps,我的Book.java,BookController.java都是放在包Test_1_Bookstore_3下面,
其余所有的.xml和jsp直接放在WEB-INF下面。
我的web.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<display-name>Test_1_Bookstore_3</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>viewspace</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>viewspace</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Book.jsp</welcome-file>
</welcome-file-list>
</web-app>
ApplicationContext.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-3.0.xsd">
</beans>
定义的viewspace-servlet如下:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0">
<context:component-scan base-package="Test_1_BookStore_3"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/"
p:suffix=".jsp" />
</beans>
Book.jsp也很简单
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
123
</body>
</html>
可是为什么tomcat如此不给面子呢。。其他项目中测试tomcat运行倒是正常的?
[解决办法]
web.xml中spring配置<!-- SpringMVC配置 -->
<servlet>
<servlet-name>spring MVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/ApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring MVC</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
[解决办法]
我今天刚刚决定放弃spring,有巨大的bug啊,目前项目暂时仅用springmvc这块。觉得老兄你也放弃吧。
[解决办法]
我想知道你运行时输入的url是什么?如果直接访问的项目根目录,根据
<welcome-file-list>
<welcome-file>Book.jsp</welcome-file>
</welcome-file-list>
</web-app>
会直接去找book.jsp
而且楼主你的web.xml和spring配置文件之间有什么关联?
你的viewspace是个servlet,spring配置文件中就找不到这个,可能你用了mvc框架我不太明白。
另外这个url-pattern,你确定只是*.html?
[解决办法]