@Controller
public class ReqAction {
@RequestMapping(value = "/req1", method = RequestMethod.GET)
public String getReq(){
System.out.println("get请求已经执行");
return "main";
}
@RequestMapping(value = "/req2", method = RequestMethod.POST)
public String posReq(){
System.out.println("post请求已经执行");
return "main";
}
}
仅限于超链接或者地址栏提交数据。
@PathVariable
@RequestParam
方法:在web.xml中设置一个中文过滤器
<filter>
<filter-name>encodefilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceRequestEncodingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>orceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodefilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
完成ajax的请求访问服务器,返回学生集合。
(1) 添加jackson依赖
(2) 在springmvc.xml文件中添加注解驱动
,它用来解析@ResponseBody注解
(3) 注册中文编码和注册springmvc
(4) 在webapp目录下新建js目录,添加jQuery函数库
(5) 在index.jsp页面上导入函数库
(6) 在action上添加注解@ResponseBody,用来处理ajax请求
【pom.xml】
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.sdnugroupId>
<artifactId>springmvc-003-ajaxartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.8version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
includes>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
includes>
resource>
resources>
build>
project>
<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"
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">
<context:component-scan base-package="com.sdnu.controller"/>
<mvc:annotation-driven>mvc:annotation-driven>
beans>
【web.xml】
<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">
<filter>
<filter-name>encodefilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceRequestEncodingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>orceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodefilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
web-app>
<%--
Created by IntelliJ IDEA.
User: Beyong
Date: 2023/4/15
Time: 17:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
index
访问服务器返回学生信息列表
等待服务器返回学生信息
package com.sdnu.controller;
import com.sdnu.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
public class StudentListAction {
@RequestMapping(value = "/list")
@ResponseBody //解析ajax请求,必须在springmvc.xml中添加注解
public List<Student> list(){
List<Student> list = new ArrayList<>();
Student stu1 = new Student("张三", 20);
Student stu2 = new Student("李四", 30);
Student stu3 = new Student("张王五", 50);
list.add(stu1);
list.add(stu2);
list.add(stu3);
return list; //springmvc负责将集合转成json数组
}
}
【JumpAction.java】
package com.sdnu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JumpAction {
@RequestMapping("/one")
public String one(){
System.out.println("这是请求转发页面跳转");
return "main"; //默认是请求转发,使用视图解析器拼接前缀和后缀后进行跳转
}
@RequestMapping("/two")
public String two(){
System.out.println("这是请求转发action跳转");
//forward可以屏蔽前缀和后缀的拼接,实现请求转发跳转
return "forward:/other.action";
}
}
【OtherAction.java】
package com.sdnu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class OtherAction {
@RequestMapping("/other")
public String other(){
System.out.println("这是other的action访问");
return "main";
}
}
【JumpAction.java】
package com.sdnu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JumpAction {
@RequestMapping("/three")
public String three(){
System.out.println("这是重定向页面跳转");
//redirect可以屏蔽前缀和后缀,实现重定向跳转
return "redirect:/admin/main.jsp";
}
@RequestMapping("/four")
public String four(){
System.out.println("这是重定向action跳转");
//redirect可以屏蔽前缀和后缀,实现重定向跳转
return "redirect:/other.action";
}
}
【OtherAction.java】
package com.sdnu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class OtherAction {
@RequestMapping("/other")
public String other(){
System.out.println("这是other的action访问");
return "main";
}
}
1)HttpServletRequest 对象
2)HttpServletResponse 对象
3)HttpSession 对象
4)Model/ModelMap 对象
5)Map
注意Model,Map,ModelMap都使用的是request请求作用域,意味着只能是请求转发后,页面才可以得到值。
【index.jsp】
<%--
Created by IntelliJ IDEA.
User: Beyong
Date: 2023/4/15
Time: 21:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/one.action">1.请求转发页面(默认)</a>
<a href="${pageContext.request.contextPath}/two.action">2.请求转发action</a>
<a href="${pageContext.request.contextPath}/three.action">3.重定向页面</a>
<a href="${pageContext.request.contextPath}/four.action">4.重定向action</a>
<a href="${pageContext.request.contextPath}/data.action?uuuName='李华'">默认参数传递数据展示</a>
</body>
</html>
【main.jsp】
<%--
Created by IntelliJ IDEA.
User: Beyong
Date: 2023/4/15
Time: 21:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>main</title>
</head>
<body>
<h1>main</h1>
httpServletRequest:${requestUser}<br/>
session:${sessionUser}<br/>
map:${mapUser}<br/>
model:${modelUser}<br/>
modelMap:${modelMapUser}<br/>
访问服务器进行数据跳转:${param.uuuName}
</body>
</html>
【DataAction.java】
package com.sdnu.controller;
import com.sdnu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class DataAction {
@RequestMapping("/data")
public String data(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
HttpSession session,
Map<String,User> map,
Model model,
ModelMap modelMap){
User u = new User("小雨", 20);
httpServletRequest.setAttribute("requestUser", u);
session.setAttribute("sessionUser", u);
map.put("mapUser", u);
model.addAttribute("modelUser", u);
modelMap.addAttribute("modelMapUser", u);
return "main";
}
}
【index.jsp】
<%--
Created by IntelliJ IDEA.
User: Beyong
Date: 2023/4/15
Time: 21:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/one.action">1.请求转发页面(默认)</a>
<a href="${pageContext.request.contextPath}/two.action">2.请求转发action</a>
<a href="${pageContext.request.contextPath}/three.action">3.重定向页面</a>
<a href="${pageContext.request.contextPath}/four.action">4.重定向action</a>
<a href="${pageContext.request.contextPath}/data.action?uuuName='李华'">默认参数传递数据展示</a><br/>
<br/><br/>
<form action="${pageContext.request.contextPath}/myDate.action">
日期:<input type="date" name="myDate"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
【DateAction.java】
package com.sdnu.controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class DateAction {
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
@RequestMapping("/myDate")
public String myDate(@DateTimeFormat(pattern = "yyyy-MM-dd")Date myDate){
System.out.println(s.format(myDate));
return "show";
}
}
【springmvc.xml】
<mvc:annotation-driven></mvc:annotation-driven>
另一个方法:
在类的成员setXXX()方法上使用@DateTimeFormat注解
【DateAction.java】
package com.sdnu.controller;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class DateAction {
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
@InitBinder
public void initBinder(WebDataBinder dataBinder){
dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(s, true));
}
@RequestMapping("/myDate")
public String myDate(@DateTimeFormat(pattern = "yyyy-MM-dd")Date myDate){
System.out.println(s.format(myDate));
return "show";
}
}
(1)JSON中的日期显示
需要在类中的成员变量的getXXX方法上加注解.
(2)JSP页面的日期显示
需要使用国际化标签,先添加依赖
很多企业会将动态资源放在WEB-INF目录下,这样可以保证资源的安全性。在WEB-INF目录下的动态资源不可以直接访问,必须要通过请求转发的方式进行访问。这样避免了通过地址栏直接对资源的访问。重定向也无法访问动态资源。