当前位置:编程学习 > JAVA >>

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? --------------------编程问答--------------------
引用 3 楼 coolbamboo2008 的回复:
我想知道你运行时输入的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?


我是直接在Eclipse下面运行的。 --------------------编程问答--------------------
引用 2 楼 yys79 的回复:
我今天刚刚决定放弃spring,有巨大的bug啊,目前项目暂时仅用springmvc这块。觉得老兄你也放弃吧。



不要这样啊。。。  无数的兄弟啊、、、 --------------------编程问答--------------------   没有看错的话,你根本就没有说出来你的错误信息。。。。 --------------------编程问答--------------------
引用 4 楼 Lucifer2603 的回复:
Quote: 引用 3 楼 coolbamboo2008 的回复:

我想知道你运行时输入的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?


我是直接在Eclipse下面运行的。


你确定你这样写别人书上的可行么,不可行就自己写吧 --------------------编程问答--------------------
引用 5 楼 zhouren1314 的回复:
Quote: 引用 2 楼 yys79 的回复:

我今天刚刚决定放弃spring,有巨大的bug啊,目前项目暂时仅用springmvc这块。觉得老兄你也放弃吧。



不要这样啊。。。  无数的兄弟啊、、、

用了好几年了,能用我还放弃它干嘛,无奈之举,数据库事务控制那块有严重bug,而且从1.0开始一直没解决,唉,目前只用springmvc,di这块了。
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,