SpringMVC,盘他!

第一章 SpringMVC

第一节

SpringMVC 是一套功能强大,性能强悍,使用方便的优秀的 MVC 框架;(取代Struts2)

第二节(需引入springMVC包)

动态版本选用2.5,才会有web.xml
SpringMVC版本HelloWorld

首先跟Struts2一样,在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>SpringMVCHello</display-name>
  <welcome-file-list>
    <welcome-file>index.html</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>

配置springMVC框架文件

<?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.gdut"/>

    <!-- 视图解析器 (控制层处理好返回)-->
	<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>

控制层:

package com.gdut.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){
		//uiModel,专门给试图用的
		model.addAttribute("message", "SpringMvc你好!");
		return "helloWorld";
	}
}

控制层根据springMVC配置文件返回页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${message }
</body>
</html>

springMVC的请求工作流程:
springMVC启动的时候,会优先执行web.xml,根据web.xml指定的参数进行扫描springmvc指定的配置文件路径(即spring-mvc.xml),用来解析注解和扫描基础项目包
在这里插入图片描述

第二章 SpringMVC控制器

第一节:@RequestMapping 请求映射

设置请求url,用于分模块

第二节:@RequestParam 请求参数

获取url的请求参数,
@RequestParam(value=“id”,required=“false”) String id
默认是url中必须要有id这个属性,否则会报错

get请求时,获取的请求参数可以在url中看见
post请求时,就只能看表单组件的名称来获取请求参数了

第三节:ModelAndView 返回模型和视图

返回模型和视图

public ModelAndView 方法名(){
	ModelAndView mav=new ModelAndView();
	mav.addObject("studentList",studentlist);//这个是用来存储数据
	mav.setViewName("student/list");//这个是用来设置页面的名字、理解成路径
	return mav;
}

第四节:SpringMVC 对象属性自动封装

方法参数中的student就是根据页面的name属性与模型类属性名进行自动封装

	@RequestMapping("/save")
	public String save(Student student) {
		if(student.getId()!=0){
			Student s=studentList.get(student.getId()-1);
			s.setName(student.getName());
			s.setAge(student.getAge());
		}else{
			studentList.add(student);			
		}
		return "redirect:/student/list.do";
	}

第五节:SpringMVC POST 请求乱码解决

在web.xml里添加springmvc的编码过滤器

	<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>*.do</url-pattern>
	</filter-mapping>

第六节:Controller 内部转发和重定向

内部转发和重定向是针对内部控制器
内部转发可以带参数,地址不变
重定向不可以带参数,url会改变

	@RequestMapping("/save")
	public String save(Student student) {
		if(student.getId()!=0){
			Student s=studentList.get(student.getId()-1);
			s.setName(student.getName());
			s.setAge(student.getAge());
		}else{
			studentList.add(student);			
		}
		return "redirect:/student/list.do";
		//return "forward:/student/list.do";
	}

页面使用超链接时候,注意使用绝对路径 ${pageContext.request.contextPath}

第七节:SpringMvc 对 ServletAPI 的支持

要保存cookie,session,需要用servletAPI,(原生servlet)

第八节:SpringMvc 对 Json 的支持

开发更倾向于用原生的ajax+json

第三章 Restful 风格的资源 URL

第一节:Restful 风格的资源 URL 简介

要对所有资源都进行拦截,采用这种风格时候,静态资源(如图片)有人会被拦截,要对静态资源进行处理,加入新标签。

第二节:SpringMvc 对 Rest 风格的支持

第三节:@PathVariable 获取 Url 变量

获取url变量

第四节:SpringMvc 对静态资源的处理

第四章 SpringMvc 文件上传

1、要支持文件上传,需要加载一个bean,CommonsMultipartResolver类

	<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		
		<property name="defaultEncoding" value="UTF-8"/>  
	    <property name="maxUploadSize" value="10000000"/>

	</bean>

2、在lib加入两个jar包

第一节:SpringMvc 单文件上传

前台上传页面:

<form action="upload2.do" method="post" enctype="multipart/form-data">
	<table>
		<tr>
			<th colspan="2">上传文件</th>
		</tr>
		<tr>
			<td>文件一</td>
			<td>
				<input type="file" name="file"/>
			</td>
		</tr>
		<tr>
			<td>文件二</td>
			<td>
				<input type="file" name="file"/>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="上传文件"/>
			</td>
		</tr>
	</table>
</form>

控制层:

	@RequestMapping("/upload")
	public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{
		String filePath=request.getServletContext().getRealPath("/");
		System.out.println(filePath);
		file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename()));
		return "redirect:success.html";
	}

第二节:SpringMvc 多文件上传

	@RequestMapping("/upload2")
	public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception{
		String filePath=request.getServletContext().getRealPath("/");
		System.out.println(filePath);
		for(MultipartFile file:files){
			file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename()));			
		}
		return "redirect:success.html";
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值