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

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值