简体   繁体   English

如何在不使用输入标签的情况下在Spring控制器中获取动态创建的td值

[英]how to get dynamic created td value in Spring controller without using input tag

I am working on my current project module that is excel sheet modification by user, and I am stuck on getting Dynamic <td> value. 我正在开发当前的项目模块,该模块是用户对excel进行的工作表修改,而且我一直坚持获取动态<td>值。

MY JSP: 我的JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page session="true"%>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
    <form action="save" method="post" id="exceldata">
        <table border="5" id="dataTable">
            <tbody>
                <c:forEach var="data" items="${Hello.userlist}" varStatus="loop">
                    <tr>
                        <c:forEach var="innerData" items="${data}">
                            <td id="innerData" contenteditable="true">
                            <c:out value="${innerData}"></c:out></td>
                          // ${innerData} will be updated by user
                           //here i need updated td ${innerData} data
                           //i have done all the possible R&D in 2days
                           // i can not use <input> under <td>
                        </c:forEach>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
        <input type="hidden" value="${sizeOfData}" name="sizeOfData">
        <input type="submit" value="save">
    </form>
</body>
</html>

my controller: 我的控制器:

here in the my controller class and in the saveExcel i need the updated td value td 在我的控制器类和saveExcel中,我需要更新的td值td

Excel_Data ed = new Excel_Data();

@RequestMapping("/")
public ModelAndView showExcel() {
    List ll = ed.readData(FILE_NAME);

    lb.setUserlist(ll);
    home.addObject("sizeOfData", ll.size());
    home.addObject("Hello", lb);
    return home;
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveExcel(@ModelAttribute("userForm") ListBean lb) throws IOException {
     // what to do here?
}
}

I have used <input type="hidden" value=${innerData} name="innerData"> but its not sending updated data 我使用了<input type="hidden" value=${innerData} name="innerData">但是它没有发送更新的数据

Based on your question, it seems like you are trying to build Excel like functionality. 根据您的问题,似乎您正在尝试构建类似Excel的功能。 More than one cell can be modified and modifications need to be submitted back to server. 可以修改多个单元,并且需要将修改提交回服务器。

When form is posted, you can iterate through all the cells using javascript and build a json object. 发布表单后,您可以使用javascript遍历所有单元格并构建一个json对象。 For example following sudo code 例如下面的sudo代码

var cells = new Object();
for every cell
   celss[cell id] = <text content of the cell>
end for

var cellText = JSON.stringify(cells)
set the value of hidden field

On server side, your method needs to have expected input field using @RequestParam annotation. 在服务器端,您的方法需要使用@RequestParam批注具有预期的输入字段。 You can gson or such tools to parse the data. 您可以使用gson或此类工具来解析数据。 If you follow good id convention eg id can be "id__", you can parse it to figure out where the contents go. 如果遵循良好的id约定,例如id可以为“ id__”,则可以对其进行解析以找出内容的去向。

The form should be mentioned with modelAttribute, which stores the values of form into that object once after it is saved. 应当使用modelAttribute来提及表单,该属性将表单的值在保存后立即存储到该对象中。

 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <form:form method="POST" id="form" name="form" class="form" action="${pageContext.request.contextPath}/home/page2/submit" modelAttribute="data"> <table border="5" id="dataTable"> <tbody> <c:forEach var="data" items="${Hello.userlist}" varStatus="loop"> <tr> <c:forEach var="innerData" items="${data}"> <td id="innerData" contenteditable="true" path="name"> <c:out value="${innerData}"></c:out></td> </c:forEach> </tr> </c:forEach> </tbody> </table> <input type="hidden" value="${sizeOfData}" name="sizeOfData" path="name"> <input type="submit" value="save"> </form:form> 

A class should be created as following in model package of the project 在项目的模型包中应按以下方式创建一个类

public class Data {
String name;
public void setName(String name) {
    this.name = name;
}
public String getName() {
    return name;
}}

Controller will have the following methods, @RequestMapping(value="/method2/submit",method = RequestMethod.GET) public String method3(ModelMap model,HttpSession session,HttpServletRequest request) { 控制器将具有以下方法,@RequestMapping(value =“ / method2 / submit”,method = RequestMethod.GET)公共字符串method3(ModelMap模型,HttpSession会话,HttpServletRequest请求){

    model.addAttribute("data",new Data());
    return VIEWS_LOCATION +"/secondPage";
}
@RequestMapping(value="/method2/submit",method = RequestMethod.POST)
public String method3(ModelMap model, @ModelAttribute("data") Data data) {
    model.addAttribute("name",data.getName());

    return VIEWS_LOCATION +"/secondPage";
}

All the elements which are suffixed with path="name" will be automatically read by data model and are stored in string name. 带有path =“ name”后缀的所有元素将由数据模型自动读取,并存储在字符串name中。

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

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