第一章 问候SpringMVC
第一节 SpringMVC简介
SpringMVC是一套功能强大,性能强悍,使用方便的优秀的MVC框架
下载和安装Spring框架:
登录http://repo.springsource.org/libs-release-local/
选择org->springframework->spring,然后选择一个版本下载,可以是4.2.0版本
除此之外,Spring的核心容器还必须依赖commons-logging的jar包
登录http://commons.apache.org/
选择Releases->Logging,下载commons-logging工具,下载好了解压将commons-logging-1.2.jar添加到项目的类加载路径中
第二节 SpringMVC版HelloWorld实现
导入对应的jar包:
web.xml: 1 <?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>SpringMvc01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <servlet>
<!-- Servlet的名称 -->
<servlet-name>springmvc</servlet-name>
<!-- Serlvet对应的java类 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 当前Servlet的参数信息 -->
<init-param>
<!-- contextConfigLocation是参数名称,该参数的值包含SpringMVC的配置文件路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<!-- Servlet映射声明 -->
<servlet-mapping>
<!-- 请求对应的Servlet的名称 -->
<servlet-name>springmvc</servlet-name>
<!-- 监听当前域的所有请求 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="helloWorld.do">问候SpringMvc</a>
</body>
</html>
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"> <!-- 使用注解的包,包括子集 -->
<!-- Spring可以自动去扫描base-pack下面的包或者子包下面的java文件,如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="com.wishwzp"/> <!-- 视图解析器 -->
<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>
HelloWorldController.java:
package com.wishwzp.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("message", "StringMvc你好!");
return "helloWorld";
}
}
helloWorld.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message }
</body>
</html>