1 SpringMVC
1.1 引言
- java开源框架,
Spring Framework 的一个独立模块。 - MVC框架,在项目中开辟MVc层次架构
- 对控制器中的功能包装简化扩展践行工厂模式,功能架构在工厂之上
1.2 MVC架构
MVC : Model View Controller 模型 视图 控制器 模型:即业务模型,负责完成业务中的数据通信处理,对应项目中的service和dao 视图:渲染数据,生成页面。对应项目中的jsp 控制器:直接对接请求,控制MVC流程,调度模型,选择视图。对应项目中的Servlet
- MVC是现下软件开发中的最流行的代码结构形态;
- 人们根据负责的不同逻辑,将项目中的代码分成MVC 3个层次;层次内部职责单一,层次之间耦合度低;
- 符合低耦合高内聚的设计理念。也实际有利于项目的长期维护。
2 开发流程
2.1 步骤
导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
配置前端控制器web.xml
右键项目名–>添加框架支持–>web
- 作为一个MVC框架,首先要解决的是:如何能够收到请求!
- 所以MVC框架大都会设计一款前端控制器,选型在Servlet或 Filter两者之一,在框架最前沿率先工作,接收所有请求。
- 此控制器在接收到请求后,还会负责springMVc的核心的调度管理,所以既是前端又是核心。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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:mvc.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>
</web-app>
配置后端控制器
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping( "/test1")
public String hello1(){
System.out.println( "hello world" );
return "hello";
}
}
配置文件
在resource目录下创建mvc.xml
默认名称:核心控制器名-servet.xml默认位置:WEB-INF随意名称: mvc.xml
随意位置:resources但需要配置在核心控制器中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="net.lj"/>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
运行Tomcat测试
http://localhost:8080/
http://localhost:8080/hello/test1
2.3 中文乱码问题
页面字符集统一
JSP <%@ page contentType="text/html;charset=UTF-8" language="java" %>
HTML <meta charset="UTF-8">
tomcat中字符集设置,对get请求中,中文参数乱码有效
Tomcat配置:URIEncoding=utf-8
设置此filter,对post请求中,中文参数乱码有效
<filter>
<filter-name>Character Encoding</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>Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.请求参数接收
3.1 基本类型参数
请求参数与方法形参同名
- springMVC默认可以识别的日期字符串格式为: YYYY/MM/dd HH:mm:ss
- 通过@DateTimeFormat可以修改默认日志格式
@RequestMapping("test1")
public String test1(Integer id, String name, Boolean gender, Date birth) {
System.out.println("test1");
System.out.println(id+","+name+","+gender+","+birth);
return "hello";
}
3.2 实体类接收(推荐)
传入值与类属性同名
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private Boolean gender;
private Date birth;
}
@RequestMapping("test2")
public String test2(User user) {
System.out.println("test2");
System.out.println(user);
return "hello";
}
3.3 数组接收
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/test3">
<input type="checkbox" name="hobby" value="football">足球
<input type="checkbox" name="hobby" value="basketball">篮球
<input type="checkbox" name="hobby" value="volleyball">排球<br/>
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("test3")
public String test3(String[] hobby) {
System.out.println("test3");
for (String s : hobby) {
System.out.println(s);
}
return "hello";
}
3.4 集合接收
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/test4">
id:<input type="text" name="user[0].id"><br>
name:<input type="text" name="user[0].name"><br>
gender:<input type="text" name="user[0].gender"><br>
<hr>
id:<input type="text" name="user[1].id"><br>
name:<input type="text" name="user[1].name"><br>
gender:<input type="text" name="user[1].gender"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("test4")
public String test4(UserList userList) {
for (User user : userList.getUsers()) {
System.out.println(user);
}
return "hello";
}
3.5 路径接收
@RequestMapping("test5/{id}")
public String test5(@PathVariable("id") Integer id) {
System.out.println(id);
return "hello";
}
@RequestMapping("test6/{id}/{name}")
public String test6(@PathVariable("id") Integer id,@PathVariable("name") String name2) {
System.out.println(id);
System.out.println(name2);
return "hello";
}
4 跳转
4.1 转发
@RequestMapping("test1")
public String test1(){
System.out.println("test1");
return "forward:/hello";
}
@RequestMapping("test2")
public String test2(){
System.out.println("test2");
return "forward:test1";
}
4.2 重定向
@RequestMapping("test3")
public String test3(){
System.out.println("test3");
return "redirect:/hello";
}
@RequestMapping("test4")
public String test4(){
System.out.println("test4");
return "redirect:/test3";
}
4.3 细节
- 在增删改之后,为了防止请求重复提交,重定向跳转
- 在查询之后,可以做转发跳转
5 传值
C得到数据后,跳转到v,并向传递数据。进而V中可以渲染数据,让用户看到含有数据的页面
- 转发跳转:Request作用域
- 重定向跳转:Session作用域
导入依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
5.1 request和session
@RequestMapping("test1")
public String test1(HttpServletRequest request, HttpSession session) {
System.out.println("test1");
request.setAttribute("name", "张三");
session.setAttribute("age", "18");
return "data";
}
name:${requestScope.name}<br>
age:${sessionScope.age}
5.2 model
@RequestMapping("test2")
public String test2(Model model) {
System.out.println("test2");
model.addAttribute("gender", true);
return "data2";
}
gender:${requestScope.gender}
5.3 @SessionAttributes
@Controller
@SessionAttributes(names = {"city","street"})
public class DataController {
@RequestMapping("test2")
public String test2(Model model) {
System.out.println("test2");
model.addAttribute("city", "北京");
model.addAttribute("street", "长安街");
return "data2";
}
@RequestMapping("test3")
public String test3(SessionStatus status) {
status.setComplete();
return "data2";
}
}
city:${sessionScope.city}
street:${sessionScope.street}
5.4 ModelAndView
@RequestMapping("test4")
public ModelAndView test4() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("forward:/hello.jsp");
modelAndView.addObject("clz", "001");
return modelAndView;
}
clz:${requestScope.clz}
6 静态资源
静态资源: html,js文件,css文件,图片文件
静态文件没有url-pattern,所以默认是访问不到的,之所以可以访问,是因为,tomcat中有一个全局的servlet:org.apache.catalina.servlets.DefaultServlet,它的url-pattern是"/",是全局默认的Servlet. 所以每个项目中不能匹配的静态资源的请求,有这个Servlet来处理即可。
但,在SpringMVC中DispatcherServlet也采用了“P”作为url-pattern,则项目中不会再使用全局的Serlvet,则静态资源不能完成访问。
6.1 解决方法1
DispathcerServlet采用其他的url-pattern
此时,所有访问handler的路径都要以.action 结尾!!
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
6.2 解决方法2
DispathcerServlet的url-pattern依然采用"/",但追加配置
<mvc:default-servlet-handler/>
6.3 解决方法3
<mvc:resources mapping="/html/**" location="/HTML/"/>
7 JSON处理
springMVC默认的Json解决方案选择是Jackson,所以只需要导入jackson的jar,即可使用。
7.1 导入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
7.2 @ResponseBody
将handler的返回值转换成JSON,并将JSON响应给客户端
当返回值本身不是字符串时,将返回值转换为JSON
@RequestMapping("test1")
@ResponseBody
public User test1(){
System.out.println("test1");
User user = new User(1, "张三");
return user;
}
@RequestMapping("test2")
@ResponseBody
public List<User> test2(){
System.out.println("test1");
User user = new User(1, "张三");
User user2 = new User(2, "李四");
List<User> users = Arrays.asList(user, user2);
return users;
}
7.3 @RestController
当类中方法都需要加上@ResponseBody 时,可以用@RestController 替代Controller
7.4 @RequestBody
<!--发送数据-->
<input type="button" value="ajax" οnclick="send_json()">
<script src="${pageContext.request.contextPath}/js/jquery-3.5.1.min.js"></script>
<script>
function send_json() {
var user={id:1, name: "shine"}
var userJson = JSON.stringify(user);
$.ajax({
user: "${pageContext.request.contextPath}/test4",
type: "post",
data: userJson,
contentType:"application/json",
success:function (ret) {
alert(ret)
}
})
}
</script>
@RequestMapping("test4")
public String test4(@RequestBody User user) {
System.out.println(user);
return "ok";
}
7.5 Jackson常用注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@JsonProperty("id2")
private Integer id;
@JsonIgnore
private String name;
@JsonFormat(pattern = "yyy-MM-dd hh:mm:ss",timezone = "GMT+8")
private Date birth;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<String> hobby;
@JsonSerialize(using = MySerializer.class)
private Double salary = 10000.126;
}
public class MySerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator gen,
SerializerProvider serializer) throws IOException {
String number = BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
gen.writeNumber(number);
}
}
7.6 FastJson
导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.52.sec06</version>
</dependency>
安装FastJson
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
使用
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User2 {
@JSONField (serialize = false)
private Integer id;
@JSONField (name="NAME",serialzeFeatures = SerializerFeature.WriteNullStringAsEmpty)
private String name ;
@JSONField (serialzeFeatures = SerializerFeature.WriteMapNullValue)
private String city;
@JSONField ( format="yyyy/MM/dd")
private Date birth;
@JSONField(serializeUsing = MySerializer2.class)
private Double salary;
}
public class MySerializer2 implements ObjectSerializer {
public void write(JSONSerializer jsonSerializer, Object object, Object o1,
Type type, int i) throws IOException {
Double value = (Double) object;
String text = value + "元";
jsonSerializer.write(text);
}
}
常用注解
- 日期格式化:@JSONField(format=“yyyy/MM/dd”)属性名修改:@JSONField(name=“birth”)
- 忽略属性:@JSONField(serialize = false)
- 包含null值: @JSONField(serialzeFeatures = SerializerFeature.WriteMapNulValue)默认会忽略所有null值,有 此注解会输出null@JSONField(serialzeFeatures = SerializerFeature.WriteNullStringAsEmpty) null的String输出为""
- 自定义序列化: @JSONField(serializeUsing = MySerializer2.class)
8 异常解析器
Controller中的每个Handler自己处理异常
此种处理方案,异常处理逻辑,分散在各个handler中,不利于集中管理
public class MyExceptionResolve implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
if (e instanceof MyException1) {
modelAndView.setViewName("redirect:/error1");
} else if (e instanceof MyException2){
modelAndView.setViewName("redirect:/error12");
} else if (e instanceof MyException3){
modelAndView.setViewName("redirect:/error3");
}
return modelAndView;
}
}
<bean class="net.lj.resolve.MyExceptionResolve"/>
throw new MyException1("test1")
9 拦截器
作用:抽取handler中的冗余功能
9.1 定义拦截器
执行顺序: preHandle–postHandle–afterCompletion
public class MyInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if (session.getAttribute("state") != null) {
return true;
}
response.sendRedirect("/login.jsp");
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
9.2 配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/inter/test1 " />
<mvc:mapping path= "/inter/test2" />
<mvc:mapping path="/inter/test*" />
<mvc:mapping path= "/inter/**" />
<mvc:exclude-mapping path=" /inter/a/**"/>
<bean class="net.lj.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
10 上传
10.1 导入依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
10.2 表单
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
file:<input type="file" name="source"><br>
<input type="submit" value="上传">
</form>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
public class UploadController {
@RequestMapping("/test1")
public String test1(MultipartFile source, HttpSession session) throws Exception{
System.out.println("test1");
String filename = source.getOriginalFilename();
String uniqueFileName = UUID.randomUUID().toString();
String ext = FilenameUtils.getExtension(filename);
String uniqueFileName2 = uniqueFileName + "." + ext;
String contentType = source.getContentType();
System.out.println(filename);
System.out.println(contentType);
String realPath = session.getServletContext().getRealPath("/upload");
System.out.println(realPath);
source.transferTo(new File(realPath + "\\" + uniqueFileName2));
return "index";
}
}
public class MyInterceptor implements HandlerInterceptor {
private Long maxFileUploadSize;
public Long getMaxFileUploadSize() {
return maxFileUploadSize;
}
public void setMaxFileUploadSize(Long maxFileUploadSize) {
this.maxFileUploadSize = maxFileUploadSize;
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
ServletRequestContext servletRequestContext = new ServletRequestContext(request);
long l = servletRequestContext.contentLength();
if (l > 1048576) {
throw new MaxUploadSizeExceededException(1048576);
}
return true;
}
}
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/test1"/>
<bean class="net.lj.interceptor.MyInterceptor">
<property name="maxFileUploadSize" value="1048576"/>
</bean>
</mvc:interceptor>
</mvc:interceptors>
11.下载
<a href="${pageContext.request.contextPath}/download1?name=jquery-3.5.1.min.js">下载</a>
@RequestMapping("/download1")
public void test1(String name, HttpSession session, HttpServletResponse response)throws IOException {
String realPath = session.getServletContext().getRealPath("/upload");
String filePath = realPath + "//" + name;
response.setHeader("content-disposition","attachment;filename="+name);
IOUtils.copy(new FileInputStream(filePath), response.getOutputStream());
}
12.验证码
导入依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
web.xml中配置servlet
<servlet>
<servlet-name>cap</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</param-value>
</init-param>
<init-param>
<param-name>kaptcha.background.clear.to</param-name>
<param-value>211,229,237</param-value>
</init-param>
<init-param>
<param-name>kaptcha.session.key</param-name>
<param-value>kaptcha</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cap</servlet-name>
<url-pattern>/captcha</url-pattern>
</servlet-mapping>
Constant | 默认值 | 描述 |
---|
kaptcha.border | yes | 图片边框,合法值:yes , no | kaptcha.border.color | black | 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue | kaptcha.border.thickness | 1 | 边框厚度,合法值:>0 | kaptcha.image.width | 200 | 图片宽 | kaptcha.image.height | 50 | 图片高 | kaptcha.producer.impl | com.google.code.kaptcha.impl.DefaultKaptcha | 图片实现类 | kaptcha.textproducer.impl | com.google.code.kaptcha.text.impl.DefaultTextCreator | 文本实现类 | kaptcha.textproducer.char.string | abcde2345678gfynmnpwx | 文本集合,验证码值从此集合中获取 | kaptcha.textproducer.char.length | 4 | 验证码长度 | kaptcha.textproducer.font.names | Arial, Courier | 字体 | kaptcha.textproducer.font.size | 40px. | 字体大小 | kaptcha.textproducer.font.color | black | 字体颜色,合法值: r,g,b 或者 white,black,blue | kaptcha.textproducer.char.space | 2 | 文字间隔 | kaptcha.noise.impl | com.google.code.kaptcha.impl.DefaultNoise | 干扰实现类 | kaptcha.noise.color | black | 干扰 颜色,合法值: r,g,b 或者 white,black,blue | kaptcha.obscurificator.impl | com.google.code.kaptcha.impl.WaterRipple | 图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple, 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy, 阴影com.google.code.kaptcha.impl.ShadowGimpy | kaptcha.background.impl | com.google.code.kaptcha.impl.DefaultBackground | 背景实现类 | kaptcha.background.clear.from | light grey | 背景颜色渐变,开始颜色 | kaptcha.background.clear.to | white | 背景颜色渐变, 结束颜色 | kaptcha.word.impl | com.google.code.kaptcha.text.impl.DefaultWordRenderer | 文字渲染器 | kaptcha.session.key | KAPTCHA_SESSION_KEY | session key | kaptcha.session.date | KAPTCHA_SESSION_DATE | session date |
编辑前端页面
<form action="${pageContext.request.contextPath}/testCaptcha">
<img src="${pageContext.request.contextPath}/captcha" id="cap" onclick="refresh()"/>
<input type="text" name="captcha">
<br>
<input type="submit" value="提交">
</form>
<script>
function refresh() {
var img = document.getElementById("cap");
img.src = "${pageContext.request.contextPath}/captcha?" + new Date().getTime();
}
</script>
编辑比对方法
@RequestMapping("testCaptcha")
public String testCaptcha(String cap, HttpSession session) {
String realCap = (String) session.getAttribute("captcha");
if (realCap.equalsIgnoreCase(cap)) {
return "index";
}
return "error1";
}
13.REST
13.1 概念
是一种开发风格,遵从此风格开发软件,符合REST风格,则RESTFUL。
两个核心要求:
- 每个资源有唯一标识
- 不同的行为,使用对应的http-method
访问标识 | 资源 |
---|
http://localhost:8989/xxx/users | 所有用户 | http://localhost:8989/xxx/users/1 | 用户1 | http://localhost:8989/xxx/users/1/orders | 用户1的所有订单 |
请求方式 | 标识 | 意图 |
---|
GET | http://localhost:8989/xxx/users | 查询所有用户 | POST | http://localhost:8989/xxx/users | 在所有用户中增加一个 | PUT | http://localhost:8989/xxx/users | 在所有用户中修改一个 | DELETE | http://localhost:8989/xxx/users/1 | 删除用户1 | GET | http://localhost:8989/xxx/users/1 | 查询用户1 | GET | http://localhost:8989/xxx/users/1/orders | 查询用户1的所有订单 | POST | http://localhost:8989/xxx/users/1/orders | 在用户1的所有订单中增加一个 |
优点:
- 看URL知道要什么
- 看http-method知道干什么
13.2 用法
Controller方法
@RestController
public class MyRestController {
@GetMapping("/users")
public List<User> queryUsers() {
System.out.println("query users");
User user1 = new User(1, "张三");
User user2 = new User(2, "李四");
return Arrays.asList(user1, user1);
}
@GetMapping("/user/{id}")
public User queryOne(@PathVariable Integer id) {
System.out.println("=====" + id);
return new User(1, "张三");
}
@DeleteMapping("/user/{id}")
public String deleteOne(@PathVariable Integer id) {
System.out.println("delete:====" + id);
return "ok";
}
@PostMapping("/users")
public String insertUser(@RequestBody User user) {
System.out.println("insert==" + user);
return "ok";
}
@PutMapping("/users")
public String updateUser(@RequestBody User user) {
System.out.println("update" + user);
return "ok";
}
}
前端页面请求
<input type="button" value="queryAll" οnclick="queryAll()">
<input type="button" value="queryOne" οnclick="queryOne()">
<input type="button" value="insertUser" οnclick="insertUser()">
<input type="button" value="updateUser" οnclick="updateUser()">
<input type="button" value="deleteUser" οnclick="deleteUser()">
<script>
function queryAll() {
$.ajax({
type: "get",
url: "${pageContext.request.contextPath}/users",
success:function (ret) {
console.log("查询所有");
console.log(ret);
}
});
}
function queryOne() {
$.ajax({
type: "get",
url: "${pageContext.request.contextPath}/users/100",
success:function (ret) {
console.log("查询单个用户");
console.log(ret);
}
});
}
function insertUser() {
var user = {name:"shine", birth: "2020-12-12 12:12:20"};
$.ajax({
type: "post",
url: "${pageContext.request.contextPath}/users",
data:JSON.stringify(user), //将JSON对象转换为JSON字符串
contentType:"application/json", //两步对应@RequestBody
success:function (ret) {
console.log("增加用户:");
console.log(ret);
}
});
}
function updateUser() {
var user = {id:1,name:"shine", birth: "2020-12-13 12:12:20"};
$.ajax({
type: "put",
url: "${pageContext.request.contextPath}/users",
data:JSON.stringify(user), //将JSON对象转换为JSON字符串
contentType:"application/json", //两步对应@RequestBody
success:function (ret) {
console.log("更新用户:");
console.log(ret);
}
});
}
function deleteUser() {
$.ajax({
type: "delete",
url: "${pageContext.request.contextPath}/users/200",
success:function (ret) {
console.log("删除用户:");
console.log(ret);
}
});
}
</script>
14 跨域问题
14.1 域
域:协议+IP+端口
- http://localhost:8989
- http://localhost:8080
- http://www.baidu.com:80
14.2 Ajax跨域问题
- Ajax发送请求时,不允许跨域,以防用户信息泄露。
- 当Ajax跨域请求时,响应会被浏览器拦截(同源策略),并报错。即浏览器默认不允许ajax跨域得到响应内容。
- 互相信任的域之间如果需要ajax访问,(比如前后端分离项目中,前端项目和后端项目之间),则需要额外的设置才可正常请求。
14.3 解决方法
允许其他域访问 在被访问方的Controller类上,添加注解
Ajax请求的url写完整绝对路径
@CrossOrigin("http://localhost:8080")
public class SysUserController(){
...
}
携带对方cookie,使得session可用
在访问方,Ajax中添加属性
$.ajax({
type: "post",
url: "http://localhost:8989/users/200",
xhrFields:{
withCredentials: true
},
success:function (ret) {
...
}
});
或
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
15 SpringMVC执行流程
实例、SSM实现登录
一、准备数据库
CREATE DATABASE shop CHARSET=UTF8;
USE shop;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(20) DEFAULT NULL,
`telephone` varchar(11) DEFAULT NULL,
`register_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`popedom` int(11) DEFAULT NULL COMMENT '0:管理员;1:普通用户',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `t_user` VALUES ('1', 'root', '12345', '15734345678', '2016-12-02 08:40:35', '0');
INSERT INTO `t_user` VALUES ('2', 'admin1', '11111', '13956567889', '2016-12-20 09:51:43', '1');
INSERT INTO `t_user` VALUES ('3', 'admin2', '22222', '13956678907', '2016-12-20 09:52:36', '1');
INSERT INTO `t_user` VALUES ('4', 'admin3', '33333', '15890905678', '2016-12-05 09:52:56', '1');
二、创建Maven项目并在pom中添加依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.24</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
三、在Resources目录下创建日志文件log4j.properties,以及数据库配置文件jdbc.properties
log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/shop?useSSL=false
jdbc.username = root
jdbc.password = 123456
四、给项目添加Web功能和配置Tomcat
1、右键项目名—>Add Framework Support…—>勾选Web Application,ok确认
2、右上角Add Configuration…,之后再左上角点击“+”选择Tomcat Server中的Local
3、配置Tomcat路径(已下载的),最后点击右下角修复(Fix)。
需要注意点击Fix后弹出内容的名称,因为它会加在上一个面板的URL上(删掉不加也行,高兴就好)
五、创建Bean包和User实体类
package net.lj.shop.bean;
import java.util.Date;
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
private int popedom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public int getPopedom() {
return popedom;
}
public void setPopedom(int popedom) {
this.popedom = popedom;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
", popedom=" + popedom +
'}';
}
}
六、创建Mapper包和用户映射器接口UserMapper
package net.lj.shop.mapper;
import net.lj.shop.bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 用户映射器接口
*/
@Mapper
public interface UserMapper {
User login(@Param("username") String username, @Param("password") String password);
}
七、创建service包和用户服务类 -UserService
package net.lj.shop.service;
import net.lj.shop.bean.User;
import net.lj.shop.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User login(String username, String password) {
return userMapper.login(username, password);
}
}
八、创建controller包和用户控制器 -UserController
package net.lj.shop.controller;
import net.lj.shop.service.UserService;
import net.lj.shop.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session) {
User user = userService.login(username, password);
if (user != null) {
session.setAttribute("username", username);
if (session.getAttribute("loginMsg") != null) {
session.removeAttribute("loginMsg");
}
if (user.getPopedom() == 0) {
return "backend/management";
} else {
return "frontend/index";
}
} else {
session.setAttribute("loginMsg", "用户名或密码错误!");
return "frontend/login";
}
}
@RequestMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("username");
session.invalidate();
return "frontend/login";
}
}
九、在Resources目录下创建mapper文件夹和用户映射器配置文件 UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.lj.shop.mapper.UserMapper">
<resultMap id="userMap" type="net.lj.shop.bean.User">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="telephone" column="telephone"/>
<result property="registerTime" javaType="java.util.Date" column="register_time" jdbcType="TIMESTAMP"/>
<result property="popedom" column="popedom"/>
</resultMap>
<select id="login" resultMap="userMap">
SELECT * FROM t_user WHERE username = #{username} AND password = #{password};
</select>
</mapper>
十、在Resources目录下创建config文件夹以及Spring配置文件spring-config.xml、SpringMVC配置文件spring-mvc-config.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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="net.lj.shop"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="10"/>
<property name="minIdle" value="10"/>
<property name="maxActive" value="200"/>
<property name="maxWait" value="6000"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="100"/>
<property name="filters" value="stat"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/UserMapper.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="net.lj.shop.mapper"/>
</bean>
</beans>
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
<mvc:resources mapping="/images/**" location="/WEB-INF/images/"/>
<mvc:annotation-driven/>
<mvc:view-controller path="user/login" view-name="frontend/login" />
<context:component-scan base-package="net.lj.shop.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp"/>
<context:component-scan base-package="net.lj.shop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
</beans>
十一、编辑Web部署描述文件 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>simonshop</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/views/frontend/login.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Character Encoding</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>Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
十二、创建前端页面
1、login.css
body {
margin: 0px;
text-align: center;
background: #cccccc;
}
2、check.js
function checkLoginForm() {
var username = document.getElementById("username");
var password = document.getElementById("password");
if (username.value == "") {
alert("用户名不能为空!");
username.focus();
return false;
}
if (password.value == "") {
alert("密码不能为空!");
password.focus();
return false;
}
return true;
}
3、login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<html>
<head>
<title>用户登录</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<base href="${basePath}">
<script src="js/check.js"></script>
<link href="css/login.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form id="frmLogin" action="user/login" method="post">
<table class="tb" border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号</td>
<td><input id="username" type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input id="password" type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录" οnclick="return checkLoginForm()"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
<c:if test="${loginMsg!=null}">
<script type="text/javascript">alert("${loginMsg}")</script>
<c:remove var="loginMsg"/>
</c:if>
</body>
</html>
4.index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<html>
<head>
<title>首页</title>
<base href="${basePath}">
</head>
<body>
<h3>普通用户登录成功</h3>
登录用户:${username} —— <a href="user/logout">注销</a><br/>
<img src="images/bear.jpg" width="300" height="250">
</body>
</html>
5.management.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<html>
<head>
<title>后台管理</title>
<base href="${basePath}">
</head>
<body>
<h3>管理员登录成功</h3>
管理员:${username} —— <a href="user/logout">注销</a><br/>
<img src="images/bear.jpg" width="300" height="250">
</body>
</html>
<td><input id="username" type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input id="password" type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录" onclick="return checkLoginForm()"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
~~~
4.index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<html>
<head>
<title>首页</title>
<base href="${basePath}">
</head>
<body>
<h3>普通用户登录成功</h3>
登录用户:${username} —— <a href="user/logout">注销</a><br/>
<img src="images/bear.jpg" width="300" height="250">
</body>
</html>
5.management.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<html>
<head>
<title>后台管理</title>
<base href="${basePath}">
</head>
<body>
<h3>管理员登录成功</h3>
管理员:${username} —— <a href="user/logout">注销</a><br/>
<img src="images/bear.jpg" width="300" height="250">
</body>
</html>
|