pojo:
package com.zking.pojo;
public class Point {
//坐标
private int x;
private int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
controller:
package com.zking.controller;
import com.zking.pojo.Point;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
@Controller
@RequestMapping("temp")
@SessionAttributes(value = "requestKey")
public class Temp {
@RequestMapping(value = "/seavPoint")
public String seavPoint(Point point) {
System.out.println(point);
return "index";
//转发 return "forward:/index.jsp";
// 重定向 return "redirect:/index.jsp";
}
//Map
@RequestMapping("/GetMap")
public String GetMap(Map map) {
map.put("requestKey", "requestValue");
return "index";
}
//ModelAndView
@RequestMapping("/GetModelAndView")
public ModelAndView GetModelAndView(ModelAndView modelAndView) {
//存入到request的model
modelAndView.addObject("ModelAndViewKey", "ModelAndViewValue");
//请求返回的视图
modelAndView.setViewName("index");
return modelAndView;
}
//model
@RequestMapping("/GetModel")
public String GetModel(Model m) {
m.addAttribute("ModelKey", "ModelValue");
return "index";
}
//去除注解@SessionAttributes
@RequestMapping("clearSessionAttributes")
public String clearSessionAttributes(SessionStatus sessionStatus) {
System.out.println(123);
sessionStatus.setComplete();
return "index";
}
//每次都在@RequestMapping之前执行
// @ModelAttribute("/init")
// public void init(Model model) {
// model.addAttribute("initKey", "initValue");
// }
}
springmvc.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 http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启扫描-->
<context:component-scan base-package="com.zking.controller"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载试图解析器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
jar:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
jsp:
<%--
Created by IntelliJ IDEA.
User: Z X
Date: 2019/11/28
Time: 19:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/temp/seavPoint.action" method="post">
X:<input name="x"><br/>
Y:<input name="y"><br/>
<input value="提交" type="submit" /><br/>
</form>
<br/>
<a href="/temp/GetMap.action">获取requestmap的值</a>${requestKey}<br/>
<a href="/temp/GetModelAndView.action">获取requestModelAndView的值</a>${ModelAndViewKey}<br/>
<a href="/temp/GetModel.action">获取requestModel的值</a>${ModelKey}<br/>
<hr/><hr/>
<a href="/temp/GetMap.action">获取sessionmap的值</a>${requestKey}<br/>
<a href="/temp/GetModelAndView.action">获取sessionModelAndView的值</a>${ModelAndViewKey}<br/>
<a href="/temp/GetModel.action">获取sessionModel的值</a>${ModelKey}<br/>
<a href="/temp/clearSessionAttributes.action">去除@SessionAttributes中注解的值</a><br/>
<a href="/temp/GetMap.action">得到initKey的值</a>${initKey}<br/>
</body>
</html>
这是我这次学习的一些代码希望对刚刚学习springmvc的朋友有些帮助,不足之处请大家指出。