对于每个循环,数据不会在jsp页面中显示

时间:2016-06-29 18:25:32

标签: jsp spring-mvc

我是Spring mvc的新手。目前我正在尝试创建一个网页,以使用spring mvc执行查看和创建Employee对象。但是在我的网页视图中,员工没有显示,

我的控制器如下

package com.testapp.springmvc.controllers;

import java.util.HashMap;

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.RequestMethod;

import com.testapp.springmvc..models.Employee;

@Controller
public class TestController {
    HashMap<Integer, Employee> employees = new HashMap<>();
    Integer count = 0;

    @RequestMapping(value = "/employee", method = RequestMethod.GET)
    public String employee(Model model) {
        model.addAttribute("employee", new Employee());
        model.addAttribute("listOfEmployee", employees.values());
        return "employee";
    }

    @RequestMapping(value = "/employee", method = RequestMethod.POST)
    public String addEmployee(@ModelAttribute("employee") Employee employee) {
        employees.put(++count, employee);
        return "redirect:employee";
    }
}

我的jsp视图如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form:form action="employee" commandName="employee" method="post">
        <table>
            <tr>
                <td><form:input path="id" disable="true" /></td>
            </tr>
            <tr>
                <td>Name of Employee <br> <form:input type="text"
                        path="fname" name="fname" />
                </td>
            </tr>
            <tr>
                <td>Surname of Employee <br> <form:input path="lname"
                        type="text" name="lname" />
            </tr>
            <tr>
                <td><input type="submit" value="Add Employee" /></td>
            </tr>
        </table>
    </form:form>
    <c:if test="${!empty listOfEmployee}">
        <table>
            <tr>
                <th>Employee Name</th>
                <th>Surname</th>
            </tr>
            <c:forEach items="${listOfEmployee}" var="employee">
                <tr>
                    <td>${employee.fname}</td>
                    <td>${employee.lname}</td>
                </tr>
            </c:forEach>
        </table>
    </c:if>

</body>
</html>

提前致谢。

2 个答案:

答案 0 :(得分:0)

所以hashMap永远不会被Employees Object所充实,这是正常的,因为你没有实现业务逻辑!尝试在控制器中创建Employees对象(即使不推荐)并将它们添加到hashMap中,然后在默认情况下使用GET方法加载页面“/ employee”时,它会列出您的Employess已经在你的hashMap中填满了。

答案 1 :(得分:0)

上面的代码无法正常工作,因为在我的web.xml文件中我使用的是2.3 dtd,将dtd定义升级为3.1代码后工作正常。