Spring mvc简介
- Spring Web MVC也是服务到工作者模式的实现,但进行可优化;
- 前端控制器是DispatcherServlet;
- 应用控制器为:处理映射器(Handler Mapping),视图解析器(View Resolver)
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
contextConfigLocation
/WEB-INF/applicationContext.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>//这里拦截所有
</servlet-mapping>
**dispatcher-servlet.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: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.tecode.comtroller"/>
<!--配置试图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--文件解析器1-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
<property name="defaultEncoding" value="utf-8"/>
</bean>
</beans>
controller层各种转发。上传及下载(仅供参考):
package com.tecode.comtroller;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockFilterConfig;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.portlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
/**
* Created by Administrator on 2018/12/4.
*/
@Controller
public class Index {
//modelMap方法带入参数
@RequestMapping("/Index")
public String Index(ModelMap modelMap) {
modelMap.put("user","完了");
return "down";
}
@RequestMapping("/try")
public ModelAndView test(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("ui");
modelAndView.addObject("user","modelandview");
return modelAndView;
}
/*
* 转发
* */
//在web_inf之外转发了
@RequestMapping("/forwardOne")
public String forwordOne(){
return "forward:Test.jsp";
}
//在WEB-INF之内转发
@RequestMapping("/forwardTwo")
public String forwordtwo(){
return "forward:Index";
}
/*//
重定向
到方法
*/
@RequestMapping("redirect")
public String redirect(){
return "redirect:Index";
}
/*到外部网页*/
@RequestMapping("redirects")
public String re(){
return "redirect:http://www.baidu.com";
}
//上传
@RequestMapping("/fileupload")
public String upload(@RequestParam("file")MultipartFile file , HttpServletRequest httpServletRequest, ModelMap modelMap) {
String path = httpServletRequest.getSession().getServletContext().getRealPath("/upload");
String name = file.getOriginalFilename();
name = (long) (Math.random() * System.currentTimeMillis()) + name.substring(name.lastIndexOf("."), name.length());
File file1 = new File(path + "/" + name);
try {
file.transferTo(file1);
} catch (IOException e) {
e.printStackTrace();
}
modelMap.put("path_img", name);
return "New";
}
//下载
@RequestMapping("/download")
public ResponseEntity<byte[]> saveFile(@RequestParam("name")String name,HttpServletRequest request) throws IOException {
String path = request.getServletContext().getRealPath("/images/");
HttpHeaders headers=new HttpHeaders();
File file=new File(path);
//解决中文乱码
String downloadFielName = new String(name.getBytes("UTF-8"),"iso-8859-1");
//设置相应
//返回文件内容类型以普通文件方式返回
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// 设置文件下载名
headers.setContentDispositionFormData("attachment",name);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
}
用到的jar包: