javaee springMVC Map ModelMap ModelAndView el和jstl的使用

pom依赖



<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>org.examplegroupId>
  <artifactId>TestSpringMVC3artifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>TestSpringMVC3 Maven Webappname>
  
  <url>http://www.example.comurl>

  <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-contextartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>


    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>


    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
    
    

  dependencies>

  <build>
    <finalName>TestSpringMVC3finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

controller

package com.test.controller;

import com.test.pojo.Address;
import com.test.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/users")
//指定放入model中的某个变量 存入session中
@SessionAttributes(value="sessionUser2")
public class UsersController {


    @RequestMapping("/getUser2")
    public String getUser2(Map map)
    {
        Users user=new Users(2,"xiaodaimenglaoshi","888",new Address(1,"shanghai"),new Date(),888888);

        map.put("user",user);

        return "showUser";

    }

    @RequestMapping("/getUser3")
    public String getUser3(ModelMap map)
    {
        Users user=new Users(3,"daimenglaoshi3","888",new Address(1,"shanghai"),new Date(),888888);

        map.put("user",user);

        return "showUser";

    }

    @RequestMapping("/getUser4")
    public ModelAndView getUser4()
    {
        Users user=new Users(4,"daimenglaoshi4","888",new Address(1,"shanghai"),new Date(),888888);

        //将要转发的数据和页面的名称 都存入ModelAndView对象
        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("user",user);

        modelAndView.setViewName("showUser");

        return modelAndView;
    }

    @RequestMapping("/getUser5")
    public ModelAndView getUser5()
    {
        List<Users> usersList=new ArrayList<Users>();

        usersList.add(new Users(1,"daimenglaoshi1","888",new Address(1,"shanghai"),new Date(),888888));
        usersList.add(new Users(2,"daimenglaoshi2","888",new Address(1,"shanghai"),new Date(),888888));
        usersList.add(new Users(3,"daimenglaoshi3","888",new Address(1,"shanghai"),new Date(),888888));

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("usersList",usersList);

        modelAndView.setViewName("showUsersList");

        return modelAndView;

    }

   






}

jsp

<%--
  Created by IntelliJ IDEA.
  User: HIAPAD
  Date: 2019/12/4
  Time: 16:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Titletitle>
head>
<body>

<c:forEach var="user" items="${usersList}">

      ${user.uname}<br/>

c:forEach>

body>
html>

<%--
  Created by IntelliJ IDEA.
  User: HIAPAD
  Date: 2019/12/4
  Time: 14:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
${user.uname}
body>
html>

你可能感兴趣的:(Mac开发,java-ee,java)