过滤静态资源文件
我们在jsp页面加入某些静态资源时会出现错误控制台出现异常
org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /ssm/static/img/1.png
浏览器中显示入下:
根本原因:在web.xml中配置的请求映射为所以的文件,如下图所示:
修改完成后运行结果:
解决方法:在springMVC中做如下配置,使用它进行拦截检测.jpg,.css,.js等后缀文件,不让其进入DispatcherServlet。
<mvc:default-servlet-handler/>
解决中文乱码问题
我们发现在提交请求的时候,如果输入的是中文,处理器方法获取到之后是乱码。
解决方法:就是添加 一个过滤器,为 request 对象设置编码集。只需要在 web.xml 中配置好即可。
<!--解决中文乱码-->
<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>
重定向
案例
新建Controller
package com.ff.ssm.controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@org.springframework.stereotype.Controller
@RequestMapping(path = "/login")
public class Controller {
@RequestMapping(path = "/toLogin")
public ModelAndView toLogin() {
//viewname为视图名,通过视图解析器查找jsp
ModelAndView mv = new ModelAndView("login");
return mv;
}
//ModelAndView默认为请求转发
@PostMapping(path = "login")
public ModelAndView login() {
ModelAndView mv = new ModelAndView("redirect:/login/tosuccess");
return mv;
}
//跳转页面简写方式,只返回视图名称,默认是请求转发
/*
public String login() {
return "redirect:/login/tosuccess";
}
*/
@GetMapping(path = "tosuccess")
public String tosuccess() {
return "success";
}
}
新建登录页面
<%--
Created by IntelliJ IDEA.
User: 云
Date: 2021/5/20
Time: 20:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="${path}/static/img/1.png">
<form method="post" action="/ssm/login/login">
<input type="hidden" name="mark" value="save">
账号:<input type="text" name="account">
<br>
密码:<input type="password" name="pwd">
<br>
年龄:<input name="text" name="age">
<br>
出生日期:<input name="text" name="birthday">
<br>
<input type="submit" value="登录">
</form>
</body>
</html>
新建登录成功页面
<%--
Created by IntelliJ IDEA.
User: 云
Date: 2021/5/20
Time: 21:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>欢迎您登录成功!!!</h3>
</body>
</html>
运行结果:
注意刚进去时访问的地址与点击登录后页面跳转的地址不同。
初次访问的截图:
登录成功后截图:
Ajax返回json
案例:
新建User
package com.ff.ssm.bean;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class User {
private Integer id;
private String name;
private Integer age;
//日期型的不能自动转化为String需要加 @DateTimeFormat转换
@DateTimeFormat
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public User(Integer id, String name, Integer age, Date birthday) {
this.id = id;
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
新建ajaxDemo.jsp
<%--
Created by IntelliJ IDEA.
User: 云
Date: 2021/5/22
Time: 17:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="${path}/static/js/jquery.1.8.3.min.js"></script>
<script type="text/javascript">
function save(){
$.post("${path}/login/save",$("#formID").serialize(),function(res){
if(res.code==200){
alert(res.msg)
}else{
alert(res.msg)
}
});
}
</script>
</head>
<body>
<form id="formID">
<input type="text" name="name">
<input type="text" name="age">
<input type="button" value="提交" onclick="save()">
</form>
</body>
</html>
新建Controller
package com.ff.ssm.controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
@org.springframework.stereotype.Controller
@RequestMapping(path = "/login")
public class Controller {
@GetMapping(path = "toAjaxDemo")
public String toAjaxDemo() {
return "ajaxDemo";
}
@PostMapping(path = "/save")
@ResponseBody
public Map save(String name, String age) {
Map map = new HashMap();
try{
//System.out.println(10/0);
map.put("code", 200);
map.put("msg", "操作成功");
map.put("data", new LinkedList<>().add("a"));
}catch (Exception e){
map.put("code", 500);
map.put("msg", "操作失败");
}
return map;
}
}
运行结果:
当程序中无异常时,运行结果
当程序中有异常时,运行结果