springmvc-pojo,model,map,modelandview

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的朋友有些幫助,不足之處請大家指出。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章