码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC学习第一天(基于Spring4.0)

时间:2015-09-08 22:06:14      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

 

简介:
Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一
Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口。在很多方面优秀于Struts2
支持REST风格的URL请求
采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性

简单HelloWorld例子(基于Spring4.0)
1. 八个基础jar包
- commons-logging-1.1.3.jar
– spring-aop-4.0.0.RELEASE.jar
– spring-beans-4.0.0.RELEASE.jar
– spring-context-4.0.0.RELEASE.jar
– spring-core-4.0.0.RELEASE.jar
– spring-expression-4.0.0.RELEASE.jar
– spring-web-4.0.0.RELEASE.jar
– spring-webmvc-4.0.0.RELEASE.jar

2. 在web.xml中配置dispatcherServlet
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--参数配置,spring.xml的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

3.创建HelloWorld类
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//代表是控制器
@Controller
public class HelloWorld {
//注解,请求映射
@RequestMapping("/helloworld")
public String handle() {
System.out.println("Hello World");
return "success";
}
}

4. 创建springmvc.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
<!--自动扫描-->
<context:component-scan base-package="com.jie.springmvc"></context:component-scan>
<!--视图转发配置 前缀+return+后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--请求前缀-->
<property name="prefix" value="WEB-INF/views/"></property>
<!--请求后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

5.创建index.jsp和success.jsp

6.注:可以下载springsource-tool-suit插件,方便开发spring项目
地址:http://spring.io/tools/ggts/all

SpringMVC学习第一天(基于Spring4.0)

标签:

原文地址:http://www.cnblogs.com/nianyin/p/4792962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!