繁体   English   中英

在jsp中输入hashmap属性

[英]hashmap attribute input in jsp

我的实体类别是Result

package com.domain;

import java.io.Serializable;
import java.util.HashMap;
import javax.persistence.Entity;
import javax.persistence.Id;


  @Entity
   public class Result implements Serializable {
    @Id
    private Long id;

      public Student getStudent() {
        return student;
       }

      public void setStudent(Student student) {
        this.student = student;
       }

    public HashMap<String, Integer> getMark() {
        return mark;
    }

    public void setMark(HashMap<String, Integer> mark) {
        this.mark = mark;
    }

    private Student student;
    private HashMap<String,Integer> mark;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
     }

   }

我的控制器功能来自我想显示带有结果实体的表单的地方

  @RequestMapping(value="show_mark",method = RequestMethod.GET)

      public ModelAndView show_subject_mark(){

       ModelAndView mav =new ModelAndView("add_subject_mark");

       mav.addObject("Result",new Result());

       return mav;

     }

我的JAVA服务器页面文件是

 <html>
      <head>
        </head>
         <body>
           <form:form method="post"  
                      action="add_subject_mark" 
                      commandName="Result">

           </form:form>
         </body>
    </html>

现在如何使用结果实体的hash-map属性在jsp文件中获取输入。

您将需要将JSTL合并到您的项目中以完成此操作而不使用脚本(应避免)。

在页面顶部添加指令:

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

以及您pom.xml的依赖项:

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

然后在页面上使用以下JSTL标记:

<c:forEach var="mark" items="${Result.mark}">
    <form:input path="Result.mark['${mark.key}']"/>
</c:forEach>

这可以通过两种不同的方法来完成:1)Scriplets 2)JSTL标签

1)小点心

<html>
  <head>
    </head>
     <body>
       <form:form method="post" action="add_subject_mark" commandName="Result">
            <%
                Result result = (Result) request.getAttribure("Result");
                HashMap<String,Integer> mark = result.getMark(); //There u got the HashMap

             %>
       </form:form>
     </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM