`
pupi
  • 浏览: 435710 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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

阅读更多
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起来了。


分享到:
评论
4 楼 pupi 2007-01-27  
peak 写道
spring mvc用起来挺快捷的,但是还是有很多不成熟的地方


当然,没有完美的技术。

不过可以展开说说看吗 ?
3 楼 peak 2007-01-24  
spring mvc用起来挺快捷的,但是还是有很多不成熟的地方
2 楼 pupi 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

输入合适的参数。就可以登录了。
1 楼 ll_2046 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

相关推荐

    Spring-MVC-step-by-step2.rar

    "Spring-MVC-step-by-step2.pdf"这份教程详细介绍了Spring MVC的每个步骤,包括环境搭建、Hello World示例、注解驱动开发、数据绑定、异常处理、国际化等,非常适合初学者和有一定基础的开发者学习。 六、源码分析 ...

    spring-mvc-step-by-step中文版

    ### Spring MVC Step-by-Step中文版知识点概览 #### 一、Spring框架核心概念与组成部分 **1. 控制反转(IoC)** - **定义**:控制反转是一种编程模式,通过它对象的依赖关系是由外部容器进行管理而不是由对象本身...

    spring-mvc-step-by-step.pdf

    - IDE:如Eclipse或IntelliJ IDEA,它们提供了方便的Spring MVC项目模板和集成工具。 - Maven或Gradle:构建工具,用于管理依赖关系和构建流程。 **3. 应用构建** 1. **项目目录结构**:按照Maven或Gradle的标准...

    spring-mvc-step-by-step

    ### Spring MVC Step-by-Step教程知识点总结 #### 概览 本教程旨在通过逐步指导的方式帮助读者理解并掌握Spring MVC框架的基本用法。Spring MVC是Spring框架中的一个模块,主要用于构建基于模型-视图-控制器...

    Developing a Spring Framework MVC application step-by-step

    在本文中,我们将深入探讨如何逐步开发一个基于Spring框架的MVC(Model-View-Controller)应用程序。Spring MVC是Spring框架的一个重要模块,它为构建Web应用提供了强大的支持,包括模型处理、视图渲染和控制器逻辑...

    Spring.MVC.Cookbook.1784396419

    Fast-paced, practical guide to learn how to set up Spring MVC to produce REST resources and templates as required by the latest front-end best practices Who This Book Is For If you are an experienced ...

    spring 最新框架jar

    simple step-by-step instructions. ## Building from Source The Spring Framework uses a [Gradle][]-based build system. In the instructions below, [`./gradlew`][] is invoked from the root of the source ...

    Spring入门十大问题

    - **MVC-step-by-step sample**:一个逐步介绍Spring MVC实现过程的示例项目,适合初学者跟随学习。 - **AppFuse**:一个基于Spring、Hibernate、Struts等技术的开源项目模板,提供了一种快速搭建Web应用的方法。 ...

    spring入门编程问题集锦 .doc

    - 通过Spring官方文档和示例项目学习,例如MVC-step-by-step和sample目录下的例子。 - 使用集成开源框架的AppFuse,它包含了Spring、Hibernate、JUnit等,通过源码了解Spring的实践应用。 - 阅读入门书籍,如...

    SpringBootWebApplicationStepByStep:使用Spring Boot Magic开发您的第一个Web应用程序

    您将使用Spring(依赖关系管理),Spring MVC,Spring Boot,Spring Security(身份验证和授权),BootStrap(样式页面),Maven(依赖关系管理),Eclipse(IDE)和Tomcat嵌入式Web服务器。 我们将帮助您设置其中...

    Java.EE.Development.with.Eclipse.2nd.Edition.178528534

    This guide takes a step-by-step approach to developing, testing, debugging, and troubleshooting JEE applications, complete with examples and tips. Table of Contents Chapter 1. Introducing JEE and ...

    详解springboot中的jar包部署步骤

    在 Eclipse 中,右键点击项目,选择“Run As” -&gt; “Maven Clean” -&gt; “Maven Install”,然后在项目的目标目录下生成 jar 包。 Step 2: 将 Jar 包部署到 Linux 服务器 将生成的 jar 包上传到 Linux 服务器的指定...

    JeeSite接私活的最佳助手(快速后端开发框架)

    通过整合Spring MVC、MyBatis、Apache Shiro、Ehcache以及Activiti等技术,JeeSite实现了从数据访问到业务逻辑处理再到前端展示的一整套完整的解决方案。 #### 技术栈概述 1. **Spring MVC**:作为模型-视图-控制...

    软件工程专业毕业生实习总结.doc

    1. **技术栈应用**:实习生最初参与的项目使用了Struts、MySQL、Spring和Hibernate等技术,这些都是Java Web开发中的常见框架,体现了软件工程中的分层架构思想,如MVC模式,以及ORM(对象关系映射)的概念。...

    MyEclipse6的使用指南

    - 调试:设置断点,使用Step Into、Step Over、Step Out等调试工具,查看变量值,进行线程分析。 5. **Web应用开发** - 创建动态Web项目,设置Servlet和JSP页面,理解MVC模式。 - 使用内置的Tomcat服务器进行...

Global site tag (gtag.js) - Google Analytics