论坛首页 Java企业应用论坛

spring MVC step by step (附带eclipse工程)

浏览 10533 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-01-21  
spring的很多例子都基于spring mvc,为了更好的理解spring,对spring mvc框架有所了解应该是很有帮助的。
学习spring mvc,spring官方推荐了springmvc step by step教程。该教程随sping的发行版一同发行,在${spring_home}/docs/MVC-step-by-step 下面。
我最近想看看spring mvc,就从这篇教程开始入手。的确是比较的浅显易懂,轻松带你进入spring mvc的世界。
除了spring mvc本身之外,从这个教程还可以看到一些别的有价值的内容:
<1> 单元测试。给出了3个测试类,分别对应dao, action, service。在单元测试中用到了mock对象。应该说,测试虽然很简单,但是很好地说明了单元测试应该如何写。
<2> hsqldb的用法。hsqldb是非常适合测试的数据库。掌握其使用,对我们写单元测试,应该会很有帮助的。
http://www.iteye.com/topic/45272
在例子中的hsqldb运行在standalone的模式下,意味着我们可以用hsqldb提供的db manager去查看该数据库。


因为sping发行版中没有附带这个教程的完整的源代码,所以我将自己在eclipse中的工程上传在这里,希望可以为大家节省一点时间。在我的工程文件中,对build文件做了一些改动。
环境设置:
eclipse3.2 + spring2.0.1 + java5.0 + ant1.6.2 + hsqldb1.8.0.7 + tomcat5.5
web应用程序的插件是轻量级的 sysdeo(myeclipse也不会有问题)
使用该文件的步骤:注意,要将${eclipse_workspace}替换成自己机器上的eclipse工作区的目录 !!
<1> 将该文件解压缩,并且放到${eclipse_workspace}/ 下面。添加新java project。命名为springmvc。然后一路yes,建好工程。编译此工程。应该没有编译错误。
<2> 该工程根目录下有个ant 的build.xml文件。
<3> 运行ant copylib, 将需要的lib文件copy到${eclipse_workspace}/springmvc/webapp/WEB-INF/lib下。
<4> 运行 ant createTables loadData printData, 建好hsql数据库表并且插入数据。刷新刚刚建好的工程。会发现一个db目录。其中有4个test.*的文件。这时您的hsql数据库运行在standalone模式下。
<5> 运行 ant junit。会执行3个单元测试。在${eclipse_workspace}/springmvc/src/test/目录下。应该没有错误。
<6> 在tomcat中增加一个context,相关目录直接指到你的eclipse工程目录,文件如下:
<Context path="/springmvc" docBase="${eclipse_workspace}/springmvc/webapp/" debug="0" privileged="true" reloadable="true" workDir="${eclipse_workspace}/springmvc/working" />

<7> 修改${eclipse_workspace}/springmvc/webapp/WEB-INF/springapp-servlet.xml,找到
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
      <property name="url">
        <value>jdbc:hsqldb:file: ${eclipse_workspace}/springmvc/db/test</value>
      </property>
      <property name="username"><value>sa</value></property>
      <property name="password"><value></value></property>
    </bean>

需要将url部分的${eclipse_workspace}也做一下替换。
<8> 启动项目springmvc,访问 http://localhost:8080/springmvc/hello.htm 。这个应用就可以 run起来了。


   发表时间:2007-01-21  
请问楼主,运行报错:
Buildfile: F:\workspace\springmvc\build.xml
build:
clearData:
     [echo] CLEAR DATA USING: org.hsqldb.jdbcDriver jdbc:hsqldb:db/test
      [sql] Executing commands
      [sql] Failed to execute:  DELETE FROM products
      [sql] java.sql.SQLException: Table not found in statement [ DELETE FROM products]
      [sql] 0 of 1 SQL statements executed successfully
loadData:
     [echo] LOAD DATA USING: org.hsqldb.jdbcDriver jdbc:hsqldb:db/test
      [sql] Executing commands
      [sql] Failed to execute:  INSERT INTO products (id, description, price) values(1, 'Lamp', 5.78)
      [sql] java.sql.SQLException: Table not found in statement [ INSERT INTO products]
      [sql] Failed to execute:  INSERT INTO products (id, description, price) values(2, 'Table', 75.29)
      [sql] java.sql.SQLException: Table not found in statement [ INSERT INTO products]
      [sql] Failed to execute:  INSERT INTO products (id, description, price) values(3, 'Chair', 22.81)
      [sql] java.sql.SQLException: Table not found in statement [ INSERT INTO products]
      [sql] 2 of 5 SQL statements executed successfully
junit:

BUILD FAILED
F:\workspace\springmvc\build.xml:71: Could not create task or type of type: junit.

Ant could not find the task or a class this task relies upon.

This is common and has a number of causes; the usual
solutions are to read the manual pages then download and
install needed JAR files, or fix the build file:
- You have misspelt 'junit'.
   Fix: check your spelling.
- The task needs an external JAR file to execute
     and this is not found at the right place in the classpath.
   Fix: check the documentation for dependencies.
   Fix: declare the task.
- The task is an Ant optional task and the JAR file and/or libraries
     implementing the functionality were not found at the time you
     yourself built your installation of Ant from the Ant sources.
   Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
     task and make sure it contains more than merely a META-INF/MANIFEST.MF.
     If all it contains is the manifest, then rebuild Ant with the needed
     libraries present in ${ant.home}/lib/optional/ , or alternatively,
     download a pre-built release version from apache.org
- The build file was written for a later version of Ant
   Fix: upgrade to at least the latest release version of Ant
- The task is not an Ant core or optional task
     and needs to be declared using <taskdef>.
- You are attempting to use a task defined using
    <presetdef> or <macrodef> but have spelt wrong or not
   defined it at the point of use

Remember that for JAR files to be visible to Ant tasks implemented
in ANT_HOME/lib, the files must be in the same directory or on the
classpath

Please neither file bug reports on this problem, nor email the
Ant mailing lists, until all of these causes have been explored,
as this is not an Ant bug.

Total time: 1 second
意思是说表没有找到吧,但是我执行建表的时候都成功了呀!

ant createTables loadData printData:

Buildfile: F:\workspace\springmvc\build.xml
createTables:
     [echo] CREATE TABLES USING: org.hsqldb.jdbcDriver jdbc:hsqldb:db/test
      [sql] Executing commands
      [sql] 2 of 2 SQL statements executed successfully
BUILD SUCCESSFUL
Total time: 1 second
0 请登录后投票
   发表时间:2007-01-21  

引用
ant createTables loadData printData:

Buildfile: F:\workspace\springmvc\build.xml
createTables:
[echo] CREATE TABLES USING: org.hsqldb.jdbcDriver jdbc:hsqldb:db/test
[sql] Executing commands
[sql] 2 of 2 SQL statements executed successfully
BUILD SUCCESSFUL



要直接执行这3个ant任务才可以的
ant createTables loadData printData

光执行createTables,因为没有commit和shutdown的动作,所以,不会生效。

判断表示不是真的建好了,可以直接用hsqldb提供的工具:

进入hsqldb.jar所在的目录,执行下面的命令:
java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing

输入合适的参数。就可以登录了。
0 请登录后投票
   发表时间:2007-01-24  
spring mvc用起来挺快捷的,但是还是有很多不成熟的地方
0 请登录后投票
   发表时间:2007-01-27  
peak 写道
spring mvc用起来挺快捷的,但是还是有很多不成熟的地方


当然,没有完美的技术。

不过可以展开说说看吗 ?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics