JSP使用JSTL从SpringMVCModel对象里取值显示

star2017 1年前 ⋅ 303 阅读

汇总 SpringMVC 传参给到 JSP ,JSP 使用 JSTL 表达式取值来显示的各种方式。

准备

添加Jar包,编写Controller类,编写JSP代码。

  1. Maven配置文件pom.xml添加jsp相关jar包。

    <!-- JSP相关 scop:provided 只在编译和开发使用,发布不用 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- JSP end -->
    
  2. Controller 类

    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        private final Logger logger = Logger.getLogger(this.getClass());
    
        ...执行方法...
    }
    
  3. JSP 页面引入核心标签库和格式化标签库

    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    

基本类型

Model模型是以key:value形式来存储数据的,Model的实现类ExtendedModelMap继承了ModelMapModelMap继承自LinkedHashMap

  1. java代码

    /**
     * 返回基本类型
     * @param model
     * @return
     */
    @RequestMapping("/baseType")
    public String baseType(Model model) {
        //model是用LinkHashMap存储数据,如果没有设置key,则key默认是该值的包装类名称的小写
        model.addAttribute("张三");
        model.addAttribute(56);
        model.addAttribute("name", "李四");
        model.addAttribute("age",22);
        model.addAttribute("price", 35.89);
        model.addAttribute("orTrue", true);
        return "userInfo";
    }
    
  2. jsp代码

    <body>
        <h2>
            <font color="red"><strong>基本类型</strong></font>
        </h2>
        <h3>String: ${name}</h3>
        <h3>int:${age}</h3>
        <h3>double:${price}</h3>
        <h3>boolean:${orTrue}</h3>
        <h3>default:${string}</h3>
        <h3>integer:${integer}</h3>
    </body>
    

对象类型

  1. java代码

    /**
     * 返回对象类型
     * @param model
     * @return
     */
    @RequestMapping("/userInfo")
    public String userInfoPg(Model model) {
        User user = new User();
        user.setRealName("王小二");
        user.setAccount("root");
        user.setPassword("root123");
        user.setAge(22);
        //没有设置key,默认是对象类型名称的小写
    //    model.addAttribute(user);
        model.addAttribute("user",user);
        return "userInfo";
    }
    
  2. jsp代码

    <body>
        <h2><font color="red"><strong>对象类型</strong></font></h2>
        <h3>真实姓名:${user.realName}</h3>
        <h3>登录名:${user.account}</h3>
        <h3>密  码:${user.password}</h3>
    </body>
    

List集合

基本类型集合

  1. java代码
/**
 * 返回基本类型List集合
 * @param model
 * @return
 */
@RequestMapping("/baseList")
public String baseList(Model model) {
    ArrayList<Object> arrayList = new ArrayList<>();
    arrayList.add("张三");
    arrayList.add("李四");
    arrayList.add(30);
    arrayList.add(15.67);
    arrayList.add(true);
    //没有设置key,认是stringList
//        model.addAttribute(list);
    model.addAttribute("list", arrayList);
    return "userInfo";
}

2. jsp代码
​``` jsp
<body>
    <h2>
        <font color="red"><strong>遍历基本类型List</strong></font>
    </h2>
    <h3>
        <!-- 偏历list -->
        <c:forEach var="item" items="${list }">
            <tr>
                <c:out value="${item }"></c:out>
            </tr>
        </c:forEach>
    </h3>
    <!-- 从list集合中根据索引位取值 -->
    <h3>${list[0] }</h3>
    <h3>${list[1] }</h3>
    <h3>${list[2] }</h3>
    <h3>${list[3] }</h3>
    <br />
</body>

对象类型集合

  1. java代码
/**
 * 返回对象List集合
 * @param model
 * @return
 */
@RequestMapping("/userList")
public String userList(Model model) {
    ArrayList<User> userList = new ArrayList<>();
    User user1 = new User();
    User user2 = new User();
    user1.setAccount("root");
    user1.setPassword("root123");
    user1.setAge(22);
    user2.setAccount("admin");
    user2.setPassword("admin123");
    user2.setAge(28);
    userList.add(user1);
    userList.add(user2);
    //默认key 是userList
//    model.addAttribute(userList);
    model.addAttribute("uList", userList);
    return "userInfo";
}
  1. jsp代码
<h2>
    <font color="red"><strong>遍历对象类型List</strong></font>
</h2>
<h3>${uList[0].age }</h3>
<h3>${uList[1].account }</h3>
<table border="1">
    <tr>
        <th>真实姓名</th>
        <th>账号</th>
        <th>密码</th>
        <th>年龄</th>
    </tr>
    <!-- 遍历对象list -->
    <c:forEach var="item" items="${uList}">
        <tr>
            <td><c:out value="${item.realName}"></c:out></td>
            <td><c:out value="${item.account}"></c:out></td>
            <td><c:out value="${item.password}"></c:out></td>
            <td><c:out value="${item.age}"></c:out></td>
        </tr>
    </c:forEach>
</table>

Map集合

  1. java代码
/**
 * 返回Map集合
 * @param model
 * @return
 */
@RequestMapping("/userMap")
public String userMap(Model model) {
    HashMap<String, User> userMap = new HashMap<>();
    userMap.put("user1", new User("admin1", "admin1111", 21));
    userMap.put("user2", new User("admin2", "admin2222", 22));
    userMap.put("user3", new User("admin3", "admin3333", 23));
    //没有设置key,默认key是hashMap
    model.addAttribute(userMap);
    model.addAttribute("map", userMap);
    return "userInfo";
}
  1. jsp代码
<body>
    <h2>
        <font color="red"><strong>遍历Map集合</strong></font>
    </h2>
    <table border="1">
        <tr>
            <th>key</th>
            <th>value</th>
            <th>账号</th>
            <th>密码</th>
            <th>年龄</th>
        </tr>
        <!-- 遍历对象list -->
        <c:forEach var="item" items="${map}">
            <tr>
                <td><c:out value="${item.key}"></c:out></td>
                <td><c:out value="${item.value}"></c:out></td>
                <td><c:out value="${item.value.account}"></c:out></td>
                <td><c:out value="${item.value.password}"></c:out></td>
                <td><c:out value="${item.value.age}"></c:out></td>
            </tr>
        </c:forEach>
    </table>
</body>

Array数组

  1. java代码
/**
 * 返回数组
 * @param model
 * @return
 */
@RequestMapping("/arrayType")
public String arrayType(Model model) {
    String[] strArr = new String[5];
    for (int i = 0; i < strArr.length; i++) {
        strArr[i] = String.valueOf(i * 2);
    }
    model.addAttribute(strArr);
    model.addAttribute("arr",strArr);
    return "userInfo";
}
  1. jsp代码
<body>
    <!-- 遍历数组 -->
    <c:forEach var="item" items="${arr}">
        <tr>
            <td><c:out value="${item}"></c:out></td>
        </tr> 
    </c:forEach>
</body>
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: