您的位置:首页 > 编程语言 > Java开发

SpringMVC入门例子

2015-06-22 21:49 537 查看
1.添加依赖

<properties>
<spring-version>3.2.13.RELEASE</spring-version>
</properties>

<dependencies>
<dependency>
<span style="white-space:pre">	</span><groupId>junit</groupId>
<span style="white-space:pre">	</span><artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<span style="white-space:pre">	</span>    <artifactId>spring-core</artifactId>
<span style="white-space:pre">	</span>    <version>${spring-version}</version>
<span style="white-space:pre">	</span></dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- end spring 核心依赖包 -->

<!-- start spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- START SERVLET -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>


2.在web.xml中添加DispatchServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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"> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 核心控制器 -->
<servlet>
<servlet-name>my</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>my</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


3.创建controller

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController
{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView modelAndView = new ModelAndView("cpf");
modelAndView.addObject("hello","cpf mvc");
System.out.println("enter");
return modelAndView;
}
}
其中的ModelAndView是为了指定返回的数据和返回的页面。

ModelAndView modelAndView = new ModelAndView("cpf");
指定了返回的页面名称为cpf(这里只给出名称即可,不需要指定view的路径和后缀,因为会在后面配置)

4.创建springmvc配置文件

注意:配置文件的命名必须是[servlet name]-servlet.xml的格式,否则会报错。因为web.xml中配置的servlet-name为my,所以配置文件名称为my-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 
<!-- bean name是外部访问的路径,class对应相应的controller -->
<bean name="/cpf.do" class="my.mvn.controller.HelloWorldController"></bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀,这里写了ModelAndView就不用写全路径了 -->
<property name="prefix" value="/view/" />
<!-- 后缀,这里写了ModelAndView就不用写view的类型了 -->
<property name="suffix" value=".jsp" />
</bean>

</beans>


5.编写返回的页面cpf.jsp

<html>
<body>
<h2>${hello}</h2>
</body>
</html>


至此所有编码完毕,目录结构如下:



6.测试

使用jetty启动

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: