静的リソースファイルのフィルタリング
JSP ページに静的リソースを追加すると、エラーが発生し、コンソールに例外が表示されます。
org.springframework.web.servlet.DispatcherServlet.noHandlerFound GET /ssm/static/img/1.png のマッピングがありません
ブラウザには次のように表示されます。
根本原因: 以下の図に示すように、web.xml で構成されたリクエストはすべてのファイルにマッピングされます。
変更が完了した後の実行結果:
解決策: springMVC で次の構成を作成し、それを使用して .jpg、.css、.js およびその他のサフィックス ファイルをインターセプトおよび検出し、それらが DispatcherServlet に入るのを防ぎます。
<mvc:default-servlet-handler/>
中国語の文字化け問題を解決
リクエストを送信するときに、入力が中国語の場合、プロセッサ メソッドで文字化けが発生することがわかりました。
解決策: フィルターを追加し、リクエスト オブジェクトのエンコーディング セットを設定するだけです。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>
リダイレクト
場合
新しいコントローラー
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 を返します
場合:
新しいユーザー
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>
新しいコントローラー
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;
}
}
実行結果:
プログラムに例外がない場合の実行結果
プログラムに例外がある場合の実行結果