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

【Spring MVC】教程——简单的mvc例子

2014-03-24 22:05 204 查看
1、准备工作spring mvc 必须的架包



2、下面是项目的架构



3、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<display-name>simpleSpringMCV</display-name>
<!-- 加载springmvc -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- 以.htm结尾的都被mvc拦截 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
4、配置Spring mvc的文件 这里放在config资源包下
mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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"> <mvc:annotation-driven/>
<!-- 自动扫描包 -->
<context:component-scan base-package="com.cat.spring.controller" />

<!-- mvc返回页面的配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 模板路径为WEB-INF/pages/ -->
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<!-- 视图模板后缀为.JSP -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>
5、在com.cat.spring.controller包下建一个Controller控制器
这里命名为HomeController

/**
*
*/
package com.cat.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
* @author chenlf
*
* 2014-3-24
*/
@Controller
@RequestMapping(value = "/")
public class HomeController {
// 拦截/index.htm 方法为GET的请求
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView view = new ModelAndView();
view.setViewName("index");
return view;
}

}


6、最后我们写一个jsp文件来测试一下mvc返回的视图
在WEB-INF/pages/文件夹下建一个index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Spring mvc 简单的例子</title>
</head>
<body>
<h3>
请求成功返回的界面
</h3>
</body>
</html>

7、最后在tomcat下启动 打开地址http://localhost:8080/demo_springmvc_simple/index.htm 就可以返回这样的页面



   好了,到这里简单的一个springmvc 就搭好了,这里配置的比较简单,主要是让一些初学者了解一下Spring mvc的一些简单配置,大神看到了勿喷

8、另外附一下项目demo的源代码地址http://download.csdn.net/detail/a124753561/7092845
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring MVC 例子