【问题标题】:hashmap attribute input in jspjsp中的hashmap属性输入
【发布时间】:2014-03-20 09:29:26
【问题描述】:

我的实体类是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 文件中的输入。

【问题讨论】:

    标签: java jsp input hashmap


    【解决方案1】:

    这可以通过两种不同的方法来完成 1) 脚本 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>
    

    【讨论】:

    • 我知道如何获取 hashmap 但我想知道如何在表单输入字段中使用它?
    【解决方案2】:

    您需要将 JSTL 合并到您的项目中以在不使用 scriptlet 的情况下完成此操作(应避免使用)。

    在页面顶部添加指令:

    &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;

    以及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>
    

    【讨论】:

    • 如何在表单输入字段中使用它。
    • 查看更新,但您需要在传递地图之前添加密钥。如果您知道密钥,只需将它们硬编码为:&lt;form:input path="Result.mark['keyName']"/&gt; 并忽略循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2015-02-19
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多