学习孔浩老师的SpringMVC视频教程笔记
1.当学习一个新的MVC框架时间需要从以下方面入手
(1).环境搭建(HelloWorld)
(2).如何传递参数到Controller
(3).如何从控制器获取参数
(4).如何完成文件的上传
(5).如何完成验证
(6).异常的处理
(7).深入学习一些原理和源代码
2.导入jar包
spring官方下载的dist目录中的所有文件
commons-logging-1.1.1.jar
(1).环境搭建(HelloWorld)
(2).如何传递参数到Controller
(3).如何从控制器获取参数
(4).如何完成文件的上传
(5).如何完成验证
(6).异常的处理
(7).深入学习一些原理和源代码
2.导入jar包
spring官方下载的dist目录中的所有文件
commons-logging-1.1.1.jar
3.配置web.xml
<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>
4.在WEB-INFO中建立hello-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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.artistninth.controller"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5.建立Controller
package com.artistninth.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value={"/","/hello"})
public String hello(int userId,Map map){
System.out.println("Hello");
System.out.println(userId);
map.put("hi", "world");
return "hello";
}
@RequestMapping(value={"/say"})
public String say(Model model){
model.addAttribute("hi","world");
//以object的类型作为键
model.addAttribute("spring");
return "hello";
}
@RequestMapping(value={"/req"})
public String req(HttpServletRequest request){
System.out.println(request.getParameter("userName"));
return "hello";
}
}
6.建立WEB-INF/jsp/hello.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>
Hello World.${hi }<br/>
${string }
</body>
</html>