简单的SpringMvc实例

简单的SpringMvc例子体验(eclipse环境)

1.新建一个web工程(名:springMVCDemo)

2.导入jar包

    将以下jar放入WEB-INF/lib目录下

        aopalliance-1.0.0.jar
        commons-logging.jar
        spring-aop-4.1.5.RELEASE.jar
        spring-beans-4.1.5.RELEASE.jar
        spring-context-4.1.5.RELEASE.jar
        spring-core-4.1.5.RELEASE.jar
        spring-expression-4.1.5.RELEASE.jar
        spring-web-4.1.5.RELEASE.jar
        spring-webmvc-4.1.5.RELEASE.jar

3.在web.xml(WEB-INF/web.xml)中配置Servlet,详细代码如下

<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app
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">
	<!-- springMVC配置开始 -->
	<servlet>
		<servlet-name>hello</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>hello</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- springMVC配置结束 -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app></span>
4.创建Spring MVC的xml配置文件

 在WEB-INF目录下新建hello-servlet.xml文件,使用Spring MVC最简单的配置。详细代码如下

<span style="font-size:12px;"><?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-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">
	<mvc:annotation-driven/>
	<context:component-scan base-package="com.demo"></context:component-scan>
</beans></span>

1)<mvc:annotation-driven/>:是Spring MVC提供的一键式配置方法,可以自动做一些注册组件的事情。
2)<context:component-scan>:扫描通过注释配置的类。上记配置可以扫描所有注释的类。如果想要只扫描一部分的话,可以通过context:include-filter子标签来 设置就可以。如(只扫描@Controller):

<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><span style="font-size:12px;"><context:component-scan base-package="com.demo" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan></span>
 

5.创建Controller

 在com.demo.controller包下建一个类(HelloController.java)

package com.demo.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {

	private final Log logger = LogFactory.getLog(HelloController.class);
	
	//处理head类型的"/"请求
	@RequestMapping(value={"/"},method= {RequestMethod.HEAD})
	public String head(){
		return "hello.jsp";
	}
	
	//处理get类型的"/index"和"/"请求
	@RequestMapping(value={"/index","/"},method= {RequestMethod.GET})
	public String index(Model model) throws Exception {
		logger.info("。。。。。处理 。。。。。");
		model.addAttribute("msg", "HELLO SPRINGMVC !!!");
		return "hello.jsp";
	}
	
}

6.创建View

 在根(WebContent)目录下创建jsp文件(hello.jsp)。详细文件如下:

<span style="font-size:12px;"><%@ page contentType="text/html; charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>hello</title>
</head>
<body>
${msg }
</body>
</html></span>
7.编译部署到Tomcat上就可以运行了。运行结果如下:


到这里就是一个简单的Spring MVC的实例了。工程详细请参照 springMVCDemo.rar文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值