Spring MVC
Spring MVC 处理流程
特点:
1清晰的角色划分
2强大的直接配置方式
3可适配、非侵入的controller
4可以重用的业务代码
5可指定绑定和验证
6可定制
SpringMVC 配置文件
<!-- 自动装配 -->
<context:component-scan base-package="com."></context:component-scan>
<!-- 设置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<%@ page language="java" import="java.util.*"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<a href="hello">hello SpringMvc </a>
<a href="test1"> 返回V1</a>
<a href="test2"> 返回V2</a>
<a href="test3/10/张三"> 参数</a>
<form action="doLogin" method="get">
<input type="text" name="userName">
<input type="text" name="userPwd">
<input type="submit" value="参数传递">
</form>
<a href="doLogin?userName=张宇峰&userPwd=123"> 参数占位</a>
<form action="doLogin2" method="post">
<input type="text" name="userName">
<input type="text" name="userPwd">
<input type="submit" value="参数传递">
</form>
<form action="file/upload" method="post"
enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传Excel" />
</form>
<form action="file/upload1" method="post"
enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传Excel1" />
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 分发Servlert -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动装配 -->
<context:component-scan base-package="com.jredu.action"></context:component-scan>
<!-- 设置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 上传组件 -->
<!-- 上传组件 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传的编码格式 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 设置最大上传大小 -->
<property name="maxUploadSize" value="5242880"/>
</bean>
</beans>
package com.jredu.action;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.jredu.Excal.TestExcal;
@Controller
public class HelloSpringMvc {
@RequestMapping("/hello")
public String hello() {
System.out.print("hello");
return "ok";
}
@RequestMapping("/test1")
public String text1() {
System.out.print("text1");
return "v1/proj1";
}
@RequestMapping("/test2")
public String text2() {
System.out.print("text2");
return "v2/proj2";
}
@RequestMapping("/test3/{userid}/{userName}")
public String text3(@PathVariable("userid")Integer id,
@PathVariable("userName")String name) {
System.out.print("用户ID:"+id+":名字:"+name);
return "v2/proj2";
}
@RequestMapping(value="/doLogin",method=RequestMethod.GET)
public String doLogin(String userName,String userPwd) {
System.out.println("用户名:"+userName+":密码:"+userPwd);
return "v2/proj2";
}
@RequestMapping(value="/doLogin2",method=RequestMethod.POST)
public String Login(
@RequestParam(value="userName",required=true)String userName,
@RequestParam(value="userPwd",required=true)String userPwd) {
System.out.println("用户名:"+userName+":密码:"+userPwd);
return "v2/proj2";
}
//上传EXCLE
@RequestMapping("file/upload")
public String upload(HttpSession session,
@RequestParam MultipartFile file) throws IllegalStateException,IOException{
//如果文件不为空
if(!file.isEmpty()){
//上传的服务器路径
String location=session.getServletContext().getRealPath("upload");
file.transferTo(new File(location+"/"+System.currentTimeMillis()+file.getOriginalFilename()));
}
return "v2/proj2";
}
//上传excel
@RequestMapping("file/upload1")
public String upload1(HttpSession session,
@RequestParam MultipartFile file)
throws IllegalStateException,IOException{
if (!file.isEmpty()) {
String localtion=session.getServletContext().getRealPath("upload");
String path=localtion+"/"+System
.currentTimeMillis()+file.getOriginalFilename();
System.out.println(path);
file.transferTo(new File(path));
TestExcal.start(path);
}
return "v2/proj2";
}
}