这里有新鲜出炉的精品教程, 程序狗速度看过来!
Spring Framework 开源 j2ee 框架
Spring 是什么呢? 首先它是一个开源的项目, 而且目前非常活跃; 它是一个基于 IOC 和 AOP 的构架多层 j2ee 系统的框架, 但它不强迫你必须在每一层 中必须使用 Spring, 因为它模块化的很好, 允许你根据自己的需要选择使用它的某一个模块; 它实现了很优雅的 MVC, 对不同的数据访问技术提供了统一的接口, 采用 IOC 使得可以很容易的实现 bean 的装配, 提供了简洁的 AOP 并据此实现 Transcation Managment, 等等
这篇文章主要为大家详细介绍了 springmvc+maven 搭建 web 项目的相关资料, 具有一定的参考价值, 感兴趣的小伙伴们可以参考一下
1 创建一个 maven project 为 spring1
2 进行项目的配置: 默认的 java 1.5 在 properties 中选择 project facts 项目进行配置, 反选 web 之后修改 java 环境为 1.8. 修改之后如下图:
3. 配置好的工作目录如下:
4 修改 pom.xml 文件
增加两个 jar 包: servlet 和 spring webmvc
pom.xml 如下:
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
4. 默认的配置文件是 spring-servlet.xml , 在 / web-inf 下创建配置文件, 内容如下:
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd">
- <!-- 配置扫描的包 -->
- <context:component-scan base-package="spring1.*" />
- <!-- 注册 HandlerMapperHandlerAdapter 两个映射类 -->
- <mvc:annotation-driven />
- <!-- 访问静态资源 -->
- <mvc:default-servlet-handler />
- <!-- 视图解析器 -->
- <bean
- >
- <property name="prefix" value="/WEB-INF/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
注: 在这个默认的配置过程中 程序会扫描 spring1 包中的所有的 controller
5 在 src/main/java 创建 controller
- package spring1;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class DemoController {
- @RequestMapping("/index")
- public String index(){
- return "index";
- }
- }
6 在 webapp 目录下创建 index.jsp.
- <html>
- <body>
- <h2>Hello World!</h2>
- </body>
- </html>
7 部署项目到 tomcat 上, 部署的内容如下:
8 启动 tomcat 在浏览器中输入: http://localhost:8080/spring1/
总结: 调试了好久, 终于调试出来信息啦 小小激动一下啊
来源: http://www.phperz.com/article/18/0320/355725.html