import cn.gorit.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Controller
@RequestMapping("/user")
public class UserController {
// 返回字符串
@RequestMapping("/testString")
public String testString(Model model) {
System.out.println("testString 执行了");
User user = new User(); // 实体类 User
user.setUsername("美美");
user.setPassword("123321");
user.setAge(18);
model.addAttribute("user",user);
return "success";
}
}
// 前面经过视图解析器的处理,均会跳转到 success.jsp 的界面
<h5>一、返回字符串</h5>
姓名:${user.username}</br>
密码:${user.password}</br>
年龄:${user.age}
/**
* 返回 空,如果没有返回值,
* 会默认请求 testVoid.jsp 结果是 404
* 请求转发是一次请求
* 重定向是两次请求
*/
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("testVoid 执行了");
// 1. 编写请求转发的程序
// request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
// 2. 重定向
// response.sendRedirect("../index.jsp");
// 3. 直接进行相应
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("gbk"); // 解决中文乱码
response.setContentType("text/html;charset=gbk");
out.print("你好");
out.flush();
out.close();
return;
}
// 和返回 字符串是一致的
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
System.out.println("testModelAndView 执行了");
//Spring 提供
ModelAndView mv = new ModelAndView();
User user = new User();
user.setUsername("小风");
user.setPassword("456");
user.setAge(18);
// 把 user 对象存储进 mv 对象中,也会把 user 对象存到 request 对象中
mv.addObject("user",user);
// 跳转到哪个页面
mv.setViewName("success");
return mv;
}
/**
* 使用关键字的形式进行转发或重定向
* */
@RequestMapping("/testForwardOrRedirect")
public String testForwardOrRedirect() {
System.out.println("testForwardOrRedirect 执行了");
// 请求的转发
// return "forward:/WEB-INF/pages/success.jsp";
// 重定向(返回根目录)
return "redirect:/index.jsp";
}
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.10.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.10.0version>
dependency>
<button id="btn">发送 ajax</button>
<script type="text/javascript">
// 页面加载,绑定单击事件
$(function () {
$("#btn").click(function () {
// alert("Hello");
$.ajax({
url:"user/testAjax",
type:"post",
contentType:"application/json;charset=UTF-8",
data:'{"username":"hehe","password":"122","age":30}', // 传 json 数据
dataType:"json",
success:function (data) {
// data 为服务端响应的 json 数据,进行解析
alert(data);
alert(data.username);
alert(data.password);
alert(data.age);
}
})
})
})
</script>
// 模拟异步请求相应
@RequestMapping("/testAjax")
public @ResponseBody User testAjax(@RequestBody User user) {
System.out.println("testAjax 执行了");
// 接收 客户端发送的 ajax 请求,传的是 json 字符串,后端吧 json 字符串封装到 user对象中
System.out.println(user);
// 得到 json 串,并相应处理 {"username":"hehe","password":"122","age:30}
// 做响应,模拟查询数据库
user.setUsername("hehe");
user.setAge(40);
// 做响应
return user;
}
上传依赖的 jar 包
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.3version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.5version>
dependency>文件
文件上传前端代码
<h3>文件上传h3>
<form action="user/fileupload1" method="post" enctype="multipart/form-data">
<input type="file" name="upload"/><br>
<input type="submit" value="上传">
form>
后端 controller
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/fileupload1")
public String fileUpload(HttpServletRequest request) throws Exception {
System.out.println("文件上传。。。");
// 使用 fileupload 完成文件上传
String path = request.getSession().getServletContext().getRealPath("/uploads/");
// 判断路径是否存在
File file = new File(path);
if (!file.exists()) {
// 创建该文件夹
file.mkdirs();
}
// 解析 request 对象,获取上传文件项
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析 request
List<FileItem> items = upload.parseRequest(request);
// 遍历
for (FileItem item: items) {
// 进行判断,当前的 item 对象是否为上传文件项
if (item.isFormField()) {
// 普通的表单项目
} else {
// 上传文件项
// 获取到上传文件的名称
String fileName = item.getName();
// 把每一个文件名称设置唯一值, uuid
String uuid = UUID.randomUUID().toString().replace("-","");
fileName = uuid +"_"+ fileName;
// 完成文件上传
item.write(new File(path,fileName));
// 删除临时文件
item.delete();
}
}
return "success";
}
}
上传原理
前端页面
<h3>文件上传 SpringMVCh3>
<form action="user/fileupload2" method="post" enctype="multipart/form-data">
<input type="file" name="upload"/><br>
<input type="submit" value="上传">
form>
Springmvc.xml
添加一个文件解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10240" />
bean>
controller 编写
// SpringMVC 文件上传
@RequestMapping("/fileupload2")
public String fileupload2(HttpServletRequest request, MultipartFile upload) throws Exception {
System.out.println("文件上传。。。");
// 使用 fileupload 完成文件上传
String path = request.getSession().getServletContext().getRealPath("/uploads/");
// 判断路径是否存在
File file = new File(path);
if (!file.exists()) {
// 创建该文件夹
file.mkdirs();
}
// 上传文件项
// 获取到上传文件的名称
String fileName = upload.getOriginalFilename();
// 把每一个文件名称设置唯一值, uuid
String uuid = UUID.randomUUID().toString().replace("-","");
fileName = uuid +"_"+ fileName;
// 完成文件上传
upload.transferTo(new File(path,fileName));
return "success";
}
添加 jar 包 即可,开两个 Tomcat服务器,使用不同的端口进行跨服务器上传文件
不知道为啥,我这里就总是报 500 错误
前端跳转页面
<h3>异常处理h3>
<a href="user/testException">异常处理a>
异常处理 controller 编写
@Controller
@RequestMapping("/user")
public class UserController {
// 异常处理
@RequestMapping("/testException")
public String testException() throws SysException {
System.out.println("testException。。。");
try {
// 模拟异常
int a = 10/0;
} catch (Exception e) {
// 打印异常信息
e.printStackTrace();
// 抛出自定义异常信息
throw new SysException("查询的所有用户出现了错误");
}
return "success";
}
}
自定义异常器
package cn.gorit.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 异常处理器
* */
public class SysExceptionResolver implements HandlerExceptionResolver {
/**
* 处理异常业务逻辑
* */
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
// 获取异常对象
SysException e = null;
if (ex instanceof SysException) {
e = (SysException)ex;
} else {
e = new SysException("系统正在维护");
}
// 创建 ModelAndView 对象
ModelAndView mv = new ModelAndView();
mv.addObject("errorMsg", e.getMessage());
mv.setViewName("error"); // 跳转的页面,这里要记得在 WEB-INF/pages 下编写一个 error.jsp
return mv;
}
}
package cn.gorit.exception;
/**
* 自定义异常类
* */
public class SysException extends Exception{
// 存储提示新
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public SysException(String message) {
this.message = message;
}
}
springmvc.xml 配置
<bean id="sysExceptionResolver" class="cn.gorit.exception.SysExceptionResolver">bean>
<h3>拦截器h3>
<a href="test/testIntercepter">拦截器a>
package cn.gorit.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
/**
* 拦截器
* */
@RequestMapping("/testIntercepter")
public String testIntercepter() {
System.out.println("testIntercepter。。。");
return "success";
}
}
package cn.gorit.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义拦截器
* */
public class MyInterceptor implements HandlerInterceptor {
/**
* 预处理
* return true 表示放行,执行下一个拦截器,如果没有,则执行 controller方法
* */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器执行了");
return true;
}
/**
* 后处理方法,controller 方法执行之后,success.jsp 执行之前
* */
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("拦截器执行了,succes.jsp 加载之前");
}
/**
* success.jsp 执行了,该方法会执行
* */
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("拦截器执行了 success.jsp 之后");
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="cn.gorit">context:component-scan>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:resources mapping="/js/**" location="/js/">mvc:resources>
<mvc:resources mapping="/css/**" location="/csss/">mvc:resources>
<mvc:resources mapping="/images/**" location="/images/">mvc:resources>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.gorit.interceptor.MyInterceptor">bean>
mvc:interceptor>
mvc:interceptors>
<mvc:annotation-driven/>
beans>
运行效果