作业帮 > 体裁作文 > 教育资讯

spring,mvc框架搭建

来源:学生作业帮助网 编辑:作业帮 时间:2024/09/25 05:22:32 体裁作文
spring,mvc框架搭建体裁作文

篇一:Spring MVC 框架搭建及详解

Spring MVC 环境搭建及详解

一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0)

1. jar包引入

Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、

cglib-nodep-2.1_3.jar

Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的驱动jar包

2. web.xml配置(部分)

spring org.springframework.web.servlet.DispatcherServlet

1

spring *.do

org.springframework.web.context.ContextLoaderListener

contextConfigLocation classpath:config/applicationContext.xml

3. spring-servlet.xml配置

spring-servlet这个名字是因为上面web.xml中标签配的值为spring(spring),再加上“-servlet”后缀而形成的

spring-servlet.xml文件名,如果改为springMVC,对应的文件名则为springMVC-servlet.xml。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-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/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

base-package="controller">

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />

4. applicationContext.xml配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

classpath:config/hibernate.cfg.xml

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

proxy-target-class="true"/>

篇二:Maven安装及搭建SpringMVC框架

在搭建SpringMVC框架前,先学习下maven的安装和基本操作。

在Windows上安装Maven

检查JDK安装

在安装Maven之前,首先要确认你已经正确安装了JDK。打开Windows的命令行,运行如下的命令来检查你的Java安装:

C:\>echo %JAVA_HOME%

C:\>java -version

如果Windows无法执行java命令,或者无法找到JAVA_HOME环境变量。你就需要检查Java是否安装了,或者环境变量是否设置正确。

下载Maven

下载apache-maven-3.0.3-bin.zip,解压得到文件夹apache-maven-3.0.3

本地安装 修改环境变量,增加系统变量M2_HOME,变量值为Maven的安装目录E:\develop\apache-maven-3.0.3 . 接着在系统变量中找到一个名为Path的变量,在变量值的末尾加上%M2_HOME%\bin;,注意多个值之间需要有分号隔开.

现在打开一个新的cmd窗口(这里强调新的窗口是因为新的环境变量配置需要新的cmd窗口才能生效),运行如下命令检查Maven的安装情况:

C:\>echo %M2_HOME%

C:\>mvn -v

修改仓库位置

修改仓库地址,仓库用于存放我们项目所依赖的所有jar包。

打开…\apache-maven-3.0.3\conf\目录下的setting.xml文件,设置成我们创建的仓库路径

打开eclipse工具。

先配置maven仓库路径 Window----Perferences-----java-----Build Path-----Classpath Variables

New一个变量的类路径。

Name

:M2_REPO 注意这个名字必须要大写。

Path :F:/maven/repo 点击“Folder…”找到有本地仓库的位置。

使用Archetype生成项目骨架

Maven提供了Archetype以帮助我们快速勾勒出项目骨架

在cmd窗口下,进入到准备搭建项目的目录下,执行如下命令

E:\person_doc\study_project>mvn archetype:generate

-DgroupId=com.tangq.test -DartifactId=testMvc

-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

1. 构建eclipse项目 E:\person_doc\study_project>cd testMvc E:\person_doc\study_project\testMvc>mvn install eclipse:eclipse

-Dwtpversion=2.0 -DdownloadSources=true E:\person_doc\study_project\testMvc>mvn clean compile

最后这条命令告诉maven清理输出目录target/

导入项目到eclipse

打开eclipse,选择File->Import->Existing Projects into Workspace,选到刚才生成的testMvc项目,点击’Finish’

4、快速配置

修改pom.xml

testMvc

org.apache.maven.plugins maven-compiler-plugin 2.3.2 1.6 1.6 UTF-8 org.apache.maven.plugins maven-resources-plugin 2.4.3 UTF-8

然后重新执行命令:

E:\person_doc\study_project\testMvc>mvn eclipse:eclipse -Dwtpversion=2.0

Spring MVC 框架搭建及详解

1. jar包引入

修改pom.xml

3.1.2.RELEASE1.6.4 1.0.0 5.1.9 3.5.0 2.5 1.2 2.2 ersion> junit junit 3.8.1 test org.springframework spring-core ${org.springframework.version} org.springframework spring-webmvc ${org.springframework.version}

ch.qos.logback logback-core ${ch.qos.logback} ch.qos.logback logback-classic ${ch.qos.logback} org.slf4j jcl-over-slf4j ${org.slf4j} org.apache.lucene lucene-core ${org.apache.lucene} mysql mysql-connector-java ${mysql.version} javax.servlet servlet-api ${javax.servlet} provided javax.servlet jstl ${javax.servlet.jstl}

篇三:spring mvc经典自学教程

spring mvc学习教程(一)-入门实例

引言

1.MVC:Model-View-Control

框架性质的C层要完成的主要工作:封装web请求为一个数据对象、调用业务逻辑层来处理数据对象、

返回处理数据结果及相应的视图给用户。

2.简要概述springmvc

Spring C 层框架的核心是 DispatcherServlet,它的作用是将请求分发给不同的后端处理器,也即 使用

了一种被称为Front Controller 的模式(后面对此模式有简要说明)。 Spring 的C 层框架使用了后端控

制器来、映射处理器和视图解析器来共同完成C 层框架的主要工作。并且spring 的C 层框架还真正地把

业务层处理的数据结果和相应的视图拼成一个对象,即我们后面会经常用到的

ModelAndView 对象。

一、入门实例

1. 搭建环境

在spring的官方API文档中,给出所有包的作用概述,现列举常用的包及相关作用: org.springframework.aop-3.0.5.RELEASE.jar:与Aop编程相关的包

org.springframework.beans-3.0.5.RELEASE.jar:提供了简捷操作bean的接口

org.springframework.context-3.0.5.RELEASE.jar:构建在beans包基础上,用来处理资源文件及国际

化。

org.springframework.core-3.0.5.RELEASE.jar:spring核心包

org.springframework.web-3.0.5.RELEASE.jar:web核心包,提供了web层接口

org.springframework.web.servlet-3.0.5.RELEASE.jar:web 层的一个具体实现包, DispatcherServlet也位于此包中。

后文全部在spring3.0 版本中进行,为了方便,建议在搭建环境中导入spring3.0 的所有jar 包(所

有jar 包位于dist 目录下)。

2.编写HelloWorld实例

步骤一、建立名为springMVC_01_helloword,并导入上面列出的jar 包。

步骤二、编写web.xml配置文件,代码如下:

1.

2.

3. xmlns:xsi="/xml/ns/javaee

5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

6.

7. spring

8. org.springframework.web.servlet.Dispatche

rServlet

9. 1

10.

11.

12. spring

13. *.do

14.

15.

16.

17.

复制代码

步骤三、建立spring-servlet.xml文件,它的命名规则:servlet-name-servlet.xml。它的主要代码如下:

1.

2.

3. spring,mvc框架搭建)xmlns="http://www.springframework.org/schema/beans"

4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

5. xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

6. xmlns:util="http://www.springframework.org/schema/util"

7. xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

8. http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

9. http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

10. http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-3.0.xsd">

11.

12.

13. class="org.springframework.web.servlet.handler.SimpleUrl

HandlerMapping">

14.

15.

16.

key="/hello.do">helloControl

17.

18.

19.

20.

21.

22.

复制代码

步骤四、完成HelloWord.java的编写,代码如下:

1. package controller;

2.

3. import javax.servlet.http.HttpServletRequest;

4. import javax.servlet.http.HttpServletResponse;

5.

6. import org.springframework.web.servlet.ModelAndView;

7. import org.springframework.web.servlet.mvc.Controller;

8.

9. public class HelloWord implements Controller {

10. public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response)

11. throws Exception {

12. ModelAndView mav = new ModelAndView("hello.jsp");

13. mav.addObject("message", "Hello World!");

14. return mav;

15. }

16. }

17. /*

18. * 说明:ModelAndView对象是包含视图和业务数据的混合对象,即是说通过此对象,我们

可以知道所

19. 返回的相应页面(比如这里返回hello.jsp页面),也可以在相应的页面中获取此对象所

包含的业务数据

20. (比如这里message-hello worrld)。*/

21.

复制代码

步骤五、在当前项目web根目录下编写hello.jsp,主要代码如下:

1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

2. <%

3. String path = request.getContextPath();

4. String basePath =

request.getScheme()+"://"+request.getServerName()+":"+request.getServer

Port()+path+"/";

5. %>

6.

7.

8.

9.

10.

11.

12. My JSP 'hello.jsp' starting page

13.

14.

15.

16.

17.

18.

19.

22.

23.

24.

25.

26. 获取值:${message}

27.

28.

29.

复制代码

步骤六:输入.../hello.do 进行测试。

如http://localhost/springmvc/hello.do

运行的结果是:获取值:Hello World!

4.简析spring mvc工作原理

(1)启动服务器,根据web.xml的配置加载前端控制器(也称总控制器)DispatcherServlet。在加载

时、会完成一系列的初始化动作。

(2)根据servlet的映射请求(上面的helloWorld实例中针对.do 请求),并参照“控制器

篇四:Spring MVC 框架搭建及详解

Spring MVC 框架搭建及详解(超级有用)

现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这

也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实

行的方案自然就多了。不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须

要掌握它的配置及原理。

一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0)

1. jar包引入

Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、

cglib-nodep-2.1_3.jar

Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、

antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、

jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的驱动jar包

2. web.xml配置(部分)

?

1

2

3

4

5

6

7

8

9

10

11

12

13 spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring-servlet.xml 默认 --> 1

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33 spring *.do org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:config/applicationContext.xml

3. spring-servlet.xml配置

spring-servlet这个名字是因为上面web.xml中标签配的值为

spring(spring),再加上“-servlet”后缀而形成的

spring-servlet.xml文件名,如果改为springMVC,对应的文件名则为

springMVC-servlet.xml。

?

1

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http:/4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.sp6 http://www.springframework.org/schema/aop http://www.springframework.7 http://www.springframework.org/schema/tx http://www.springframework.o8 http://www.springframework.org/schema/context

9 htt10

11

12

13

14

15 16

17

18 20

21 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27 classpath:config/hibernate.cfg.xml

29

30

31

32

33

34

35

二、详解

Spring MVC与Struts从原理上很相似(都是基于MVC架构),都有一个控制页面

请求的Servlet,处理完后跳转页面。看如下代码(注解):

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21 package controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import entity.User; @Controller //类似Struts的Action public class TestController { @RequestMapping("test/login.do") // 请求url地址映射,类似Struts的a public String testLogin(@RequestParam(value="username")String username, // @RequestParam是指请求url地址映射中必须含有的参数(除非属性re // @RequestParam可简写为:@RequestParam("username") if (!"admin".equals(username) || !"admin".equals(password)) { return "loginError"; // 跳转页面路径(默认为转发),该路缀和后缀

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61 return "loginSuccess"; } @RequestMapping("/test/login2.do") public ModelAndView testLogin2(String username, String password, int ag // request和response不必非要出现在方法中,如果用不上的话可以去 // 参数的名称是与页面控件的name相匹配,参数类型会自动被转换 if (!"admin".equals(username) || !"admin".equals(password) || ag return new ModelAndView("loginError"); // 手动实例化Mod的方法返回字符串 } return new ModelAndView(new RedirectView("../index.jsp")); // // 重定向还有一种简单写法 // return new ModelAndView("redirect:../index.jsp"); } @RequestMapping("/test/login3.do") public ModelAndView testLogin3(User user) { // 同样支持参数为表单对象,类似于Struts的ActionForm,User不需要 String username = user.getUsername(); String password = user.getPassword(); int age = user.getAge(); if (!"admin".equals(username) || !"admin".equals(password) || ag return new ModelAndView("loginError"); } return new ModelAndView("loginSuccess"); } @Resource(name = "loginService") // 获取applicationContext.xml中be private LoginService loginService; //等价于spring传统注入方式写get和得代码 @RequestMapping("/test/login4.do") public String testLogin4(User user) { if (loginService.login(user) == false) { return "loginError"; }

return "loginSuccess"; }

}

篇五:SpringMVC框架搭建

现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这

也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实

行的方案自然就多了。不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须

要掌握它的配置及原理。

一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0)

1. jar包引入

Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、

cglib-nodep-2.1_3.jar

Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、

antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、

jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的驱动jar包

2. web.xml配置(部分)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15 spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring-servlet.xml 默认 --> 1

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33 spring *.do org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:config/applicationContext.xml

3. spring-servlet.xml配置

spring-servlet这个名字是因为上面web.xml中标签配的值为

spring(spring),再加上“-servlet”后缀而形成的

spring-servlet.xml文件名,如果改为springMVC,对应的文件名则为

springMVC-servlet.xml。

?

1

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.sp4 xmlns:context="http://www.springframework.org/schema/context"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.sprin6 http://www.springframework.org/schema/aop http://www.springframework.org/sch7 http://www.springframework.org/schema/tx http://www.springframework.org/sche8 http://www.springframework.org/schema/context

9 http10

11

12

14

15 16

17

18 20

21 4. applicationContext.xml配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29 classpath:config/hibernate.cfg.xml

31

32

33

34

35

二、详解

Spring MVC与Struts从原理上很相似(都是基于MVC架构),都有一个控制页面请求的Servlet,处理完后跳转页面。看如下代码(注解):

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26 package controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import entity.User; @Controller //类似Struts的Action public class TestController { @RequestMapping("test/login.do") // 请求url地址映射,类似Struts的action- public String testLogin(@RequestParam(value="username")String username, Stri // @RequestParam是指请求url地址映射中必须含有的参数(除非属性required=f // @RequestParam可简写为:@RequestParam("username") if (!"admin".equals(username) || !"admin".equals(password)) { return "loginError"; // 跳转页面路径(默认为转发),该路径不需要包含 } return "loginSuccess"; } @RequestMapping("/test/login2.do") public ModelAndView testLogin2(String username, String password, int age){

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61 // 参数的名称是与页面控件的name相匹配,参数类型会自动被转换 if (!"admin".equals(username) || !"admin".equals(password) || age < 5) { return new ModelAndView("loginError"); // 手动实例化ModelAndView完成符串 } return new ModelAndView(new RedirectView("../index.jsp")); // 采用重定向 // 重定向还有一种简单写法 // return new ModelAndView("redirect:../index.jsp"); } @RequestMapping("/test/login3.do") public ModelAndView testLogin3(User user) { // 同样支持参数为表单对象,类似于Struts的ActionForm,User不需要任何配置 String username = user.getUsername(); String password = user.getPassword(); int age = user.getAge(); if (!"admin".equals(username) || !"admin".equals(password) || age < 5) { return new ModelAndView("loginError"); } return new ModelAndView("loginSuccess"); } @Resource(name = "loginService") // 获取applicationContext.xml中bean的id private LoginService loginService; //等价于spring传统注入方式写get和set方 @RequestMapping("/test/login4.do") public String testLogin4(User user) { if (loginService.login(user) == false) { return "loginError"; } return "loginSuccess"; }

} 以上4个方法示例,是一个Controller里含有不同的请求url,也可以采用一个url访问,通过url参数来区分访问不同的方法,代码如下:

?

1 package controller;

体裁作文