使用从Spring Controller获取的值填充表单

时间:2017-01-18 06:49:51

标签: java spring spring-mvc

我只是在Spring MVC中创建一个CRUD应用程序。我想编辑学生详细信息。我创建了一个用于添加学生的表单。如何使用相同的表单填充学生详细信息进行编辑?

控制器

{
  "joel@dept.com":
       {
            "EID":"0153"
        },
   "Stan@dept.com":
       {
               "EID":"0163"
        }
}

表格

@RequestMapping(value="/add", method = RequestMethod.GET)
public String addStudent(@RequestParam("studentName") String name,@RequestParam("studentId") String studId){
    System.out.println("Student Id : "+ studId);
    System.out.println("Student "+name+" added");
    list.add(name);
    return "redirect:get";
}

@RequestMapping(value="/edit/${index}", method = RequestMethod.GET)
public String editStudent(@PathVariable("index") int index, Model model){
    System.out.println("Edit Student with Index " + index);
    model.addAttribute("studentId",index);
    model.addAttribute("studentName",list.get(index));
    return "student";
}

我想在editStudent方法的模型中设置的表单字段中设置studentId和studentName。

3 个答案:

答案 0 :(得分:1)

您所问的是一个非常基本的问题,理想情况下应该从教程和文档中学习。

以下是一个简短的步骤列表:

  • 使用Spring tags for rendering form<form:form><form:input>等)
  • 创建一个表示表单值的对象,并将其从控制器导出到视图
  • 将此对象作为处理表单提交的控制器方法中的参数

答案 1 :(得分:0)

我认为你需要两页和一个控制器 1.列出所有学生:index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h3>Student List</h3>
    <div>
        <p>
        <ul>
            <c:forEach items="${requestScope.students}" var="student">
                <li>
                    <c:out value="${student.id}"></c:out> |
                    <c:out value="${student.name}"></c:out> |
                    <a href="${pageContext.request.contextPath}/student/<c:out value='${student.id}'/>">edit</a>
                </li>
            </c:forEach>
        </ul>
        </p>
        <p><a href="${pageContext.request.contextPath}/student/new">Create Student</a></p>
    </div>
    </body>
    </html>
  1. 用于显示或编辑或创建学生:edit.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <c:if test="${student.id == null}">
        <h3>Student Create</h3>
    </c:if>
    <c:if test="${student.id != null}">
        <h3>Student Edit</h3>
    </c:if>
    <div>
        <form action="${pageContext.request.contextPath}/student/" method="post">
            <input type="hidden" name="id" value="<c:out value='${student.id}'/>"/>
            <p>Student Name: <input type="text" name="name" value="<c:out value='${student.name}'/>"></p>
            <p><input type="submit" value="submit"/></p>
        </form>
    </div>
    </body>
    </html>
    
  2. 学生控制器

    package cn.kolbe.student;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.servlet.ModelAndView;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.ConcurrentHashMap;
    @Controller
    @RequestMapping("/student")
    public class StudentController {
        @GetMapping("")
        public ModelAndView index() {
            List<Student> students = new ArrayList<Student>();
            studentCache.keySet().forEach(id -> {
                students.add(studentCache.get(id));
            });
            return new ModelAndView("student/index", "students", students);
        }
        @GetMapping("/{id}")
        public ModelAndView show(@PathVariable("id")String id) {
            if (id.equals("new")) {
                return new ModelAndView("student/edit");
            } else {
                Student student = studentCache.get(Long.valueOf(id));
                return new ModelAndView("student/edit", "student", student);
            }
        }
        @PostMapping("")
        public String createOrEdit(String name, Long id) {
            Student student;
            if (id == null) {
                id = cacheId++;
                student = new Student(id, name);
                studentCache.put(id, student);
            } else {
                student = studentCache.get(id);
                student.setName(name);
            }
            return "redirect:/student";
        }
        private static ConcurrentHashMap<Long, Student> studentCache = new ConcurrentHashMap<>();
        private static Long cacheId = 1L;
    }
    

答案 2 :(得分:0)

请勿使用html <form>标记。

使用Spring标签呈现<form:form> ,<form:input>

表单