这里有新鲜出炉的精品教程, 程序狗速度看过来!
Spring Framework 开源 j2ee 框架
Spring 是什么呢? 首先它是一个开源的项目, 而且目前非常活跃; 它是一个基于 IOC 和 AOP 的构架多层 j2ee 系统的框架, 但它不强迫你必须在每一层 中必须使用 Spring, 因为它模块化的很好, 允许你根据自己的需要选择使用它的某一个模块; 它实现了很优雅的 MVC, 对不同的数据访问技术提供了统一的接口, 采用 IOC 使得可以很容易的实现 bean 的装配, 提供了简洁的 AOP 并据此实现 Transcation Managment, 等等
这篇文章主要介绍了详解使用 maven 一步一步构建 spring mvc 项目, 具有一定的参考价值, 感兴趣的小伙伴们可以参考一下
1 使用 eclipse 构建 maven web 项目
1.1 新建 Maven 的 web 项目
打开菜单 File New-MavenProject
点击 Next
选择模板类型 archtypemaven-archtype-webapp 然后点击 Next
输入 Group Id 和 artifact IdGroup Id 一般填入项目名称, Artifact Id 一般填入子项目的名称
生成的项目文件结构如下所示:
选择 pom.xml 文件, 并打开, 界面如下所示:
增加 Properties: 展开 Properties 选项, 然后点击 Create 按钮, 如下所示: 然后 Name 字段填入 springVersion,Value 字段填入 3.2.5.RELEASE 即在 pom.xml 中增加了一个属性 springVersion, 属性值为 3.2.5.RELEASE
选择 Dependencies 标签, 打开 Dependencies 选项卡, 并增加一个新的 Dependency
- Group Id:org.springframework
- Artifact Id:spring-web
- Version:${springVersion}
点击 ok 按钮
说明: 该过程是加入 springframe 的 spring-web 依赖库,${springVersion} 是之前设置的属性
新建 Dependency:
- Group Id:org.springframework
- Artifact Id:spring-webmvc
- Version:${springVersion}
点击 ok 按钮
说明: 该过程是加入 springframe 的 spring-webmvc 依赖库,${springVersion} 是之前设置的属性
依赖库设定完之后, 如果本地不存在还需要从网络上下载相应的依赖库, 选中 pom.xml 文件, 右击鼠标选中 Run AS maven install, 然后系统自动从网络上下载相应的依赖库
依赖库下载完之后, 可以在目录 JavaResources Liraries Maven Dependencies 中看到相应的库文件, 如下图所示:
在 src main 目录下新建文件夹 Java
在 java 中新建类 Hello.java 包名为 com.springmvc.controller
Hello.java 中的内容如下:
- package com.springmvc.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class Hello {
- @RequestMapping(value="/Hello")
- public String HelloWorld(Model model){
- model.addAttribute("message","Hello World!!!");
- return "HelloWorld";
- }
- }
在 src main webapp WEB-INF 目录下新建文件夹 view, 并新建文件 HelloWorld.jsp
Helloworld.jsp 文件内容如下所示:
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
- </head>
- <body>
- <h1>message:${message}</h1>
- </body>
- </html>
选中 web.xml 文件, 双击打开该文件, 修改该文件使其如下所示:
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0">
- <servlet>
- <servlet-name>spring-mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring-mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
在 src main webapp WEB-INF 目录下新建文件 spring-mvc-servlet.xml, 文件内容如下所示:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" 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
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <context:component-scan base-package="com.springmvc.controller" />
- <bean id="viewResolver"
- class="orgspringframeworkwebservletviewInternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/view/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
Ok, 所有文件已经建立完毕, 现在可以运行该项目, 看一下效果如何了, 选中该项目 (点击 com.liuht.springmvc, 即该项目的最顶层), 点击 Run As Run on Server
出现一个界面, 让你选中要使用的 Web 服务器, 有两个选项, 一个是已存在的服务器, 另一个是重新定一个新的服务器, 我选择已存在服务器, 如果你没有, 可以重新建立一个 web 服务器
选中要运行的项目, 点击 Add > 按钮, 添加到右边的选择框中, 如果右边有其他不需要的项目, 可以选中, 并点击 < Remove 按钮删除配置完成之后, 点击 Finish 按钮
在 Console 窗口看到如下内容, 说明项目启动成功:
Eclipse 自动打开自己的浏览器, 并显示如下内容:
你也可以打开浏览器输入 http://localhost:8080/com.liuht.springmvc/
出现这个界面说明项目已经成功了, Hello World! 这串字符来自控制器 Hello.java 文件
来源: http://www.phperz.com/article/18/0309/353472.html