一. 项目环境搭建

SpringMVC的初体验-1_xml


SpringMVC的初体验-1_springmvc_02


SpringMVC的初体验-1_mvc_03


SpringMVC的初体验-1_springmvc_04

二. 基础配置

  1. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SpringMVCDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<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:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

classpath指向项目src/目录。

  1. 配置spring-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"
xsi:schemaLocation="
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.xsd">

<!-- 使用注解的包,包括包的子路径 -->
<context:component-scan base-package="com.bee"/>

<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

context:component-scan标签定义扫描包的基路径(base-package)。
base-package下的类只要使用了注解@Controller,就会被扫描到并加入SpringMVC的IoC容器。

Spring是SpringMVC的父容器,Spring中的bean在SpringMVC中可以看到,但SpringMVC中的bean在Spring中是看不到的。

三. 在控制层定义一个测试类

  1. 测试类定义
package com.bee.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

@RequestMapping("/helloworld")
public String helloWorld(Model model) {
model.addAttribute("msg", "初恋SpringMVC");
return "helloworld";
}
}

这里Model 接口用于向View层传递参数。

  1. 定义一个测试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>HelloWorld</title>
</head>
<body>${msg }
</body>
</html>
  1. 测试结果
  2. SpringMVC的初体验-1_mvc_05


  3. SpringMVC的初体验-1_spring_06


  4. SpringMVC的初体验-1_xml_07


  5. SpringMVC的初体验-1_xml_08

可以使用一个a标签做跳转

<a href="hellowworld.do"></a>