SpringMVC学习第一天(基于Spring4.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))