简体   繁体   English

在Spring MVC中-model.addAttribute用于动态数据以及如何将数据放置在jsp文件中

[英]In Spring MVC - model.addAttribute usage for dynamic data and how to place the data in jsp file

Hi I am new to Java world and I am trying to make my own web application with Spring MVC. 嗨,我是Java世界的新手,我正在尝试使用Spring MVC制作自己的Web应用程序。 Now, I am going to read a text file in my local directory, for example, the text file like this: 现在,我将在本地目录中读取一个文本文件,例如,如下所示的文本文件:

TestData_FileOne.txt
1,100
2,200
3,300
4,400
5,500

The result I would like to present in a browser page like this (in a table) : 我想在这样的浏览器页面(在表格中)呈现的结果:

1   2   3   4   5
100 200 300 400 500

so I implemented 1) Controller , 2) Model , and 3)View(.jsp file). 所以我实现了1)Controller,2)Model和3)View(.jsp文件)。

**1) My Controller and 2)Model ([Q1] [Q2]) ** ** 1)我的控制器和2)模型([Q1] [Q2])**

@Controller
public class TestController {

   @RequestMapping("/testMVC")
   public String testmvc(Model model){
       String dirString = "C:/Users/Me/Documents/Test/";
       Path testFile;
       List<String> testData;

       testFile = Paths.get( dirString + "TestData_FileOne.txt");

       try {    
           testData = Files.readAllLines(testFile, StandardCharsets.UTF_8);
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
           //return "unable to read...";
           return "unable to read...";
       }

       // ====== changes from here based on Aeseir's answer========
       List<String> dataNum = new ArrayList<String>();
       List<String> data = new ArrayList<String>();
       for(int i=0; i<testData.size()-1;i++){
           dataNum.add(testData.get(i).split(",")[0]);
           data.add(testData.get(i).split(",")[1]);
       }
       model.addAttribute("dataNum", dataNum);
       model.addAttribute("data", data);
       // ======= changes until here ==============

       return "testMVC";   
    }
  }

(Read the text file works fine when I checked System.out.println part) (当我检查System.out.println部分时,读取文本文件工作正常)

2) testMVC.jsp file 2)testMVC.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>  

<table>
<thread>
  <tr>
    <th>Table with dynamic data from model</th>
  </tr>
</thread>
<tbody>
  <c:forEach var="dataNumValue" items="${dataNum}"> [items added but.. Q5]
    <tr>
      <td>
             ${dataNumValue}
      </td>
    </tr>
  </c:forEach>
  <c:forEach var="dataValue" items="${data}"> [items added but.. Q5]
    <tr>
      <td>
             ${dataValue}       --- [Q2']
     </td>
    </tr>
  </c:forEach>
</tbody>
</table>

So..I know Q1/Q1' should be match, and Q2/Q2' as well. 所以..我知道Q1 / Q1'应该匹配,Q2 / Q2'也应该匹配。

1) however, I am confused about the object in model.addAttribute("", object); 1)但是,我对model.addAttribute(“”,object);中的对象感到困惑; in Q1, Q2? 在第一季度,第二季度? and addAttribute is the right choice among model attributes? 和addAttribute是模型属性之间的正确选择?

2) Do I need var="dataNum" and var="data" in Q3, Q4 and did I do correctly? 2)我在第3季度,第4季度需要var =“ dataNum”和var =“ data”吗,我做得正确吗?

I appreciate any advice if I made mistake. 如果我弄错了,我将不胜感激。

Extra Question so I have updated Controller code and jsp file like the above after Aeseir's answer (Thanks!!) but I have warning in jsp file after I added items then warning (Q5) and of course, the page is not presented. 额外的问题,所以在Aeseir回答后,我已经像上面那样更新了控制器代码和jsp文件(谢谢!),但是在添加项然后发出警告(Q5)之后,我在jsp文件中发出了警告,当然,该页面未显示。 [Q5]: warning : "items" does not support runtime expressions I searched for the warning, then advices like check the jstl version - should be above version 1.0 - My jstl version is 1.2. [Q5]:警告:“项目”不支持我搜索警告的运行时表达式 ,然后建议检查jstl版本-应该高于1.0版- 我的jstl版本是1.2。 so shouldn't be any problem.... Can you check my changes part? 所以应该没有问题。... 您可以检查我的更改部分吗? and What else could cause this warning except jstl version? 并且除了jstl版本,还有什么可能导致此警告? .

Solution for extra question 5 @taglib directive should be like this in jsp file: 附加问题5 @taglib指令的解决方案应在jsp文件中如下所示:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> => /jsp was missing in the middle <%@ taglib uri =“ http://java.sun.com/jsp/jstl/core” prefix =“ c”%> =>中间缺少/ jsp

This @taglib correction + code changes the above based on Aeseir's answer works all fine! 这个@taglib更正+代码根据Aeseir的回答改变了上面的内容,一切正常!

Q1 and Q2 Q1和Q2

You were almost there. 你快到了 Model will pass most data you put into it. 模型将传递您放入其中的大多数数据。 Its up to your rendering page to determine how to display it. 由您的呈现页面决定如何显示它。

And you will need to change the types to arrays since you want to output multiple strings. 而且,由于要输出多个字符串,因此需要将类型更改为数组。

List<String> dataNum = //populate accordingly
List<String> data = // populate accoridngly

model.addAttribute("dataNum", dataNum);
model.addAttribute("data", data);

Q3 and Q4 第三季度和第四季度

Yes you do but you need to complete it this way: 是的,您可以,但是您需要通过以下方式完成它:

<c:forEach var="dataNumValue" items="${dataNum}">   
    <tr>
      <td>
             ${dataNumValue}    
      </td>
    </tr>
  </c:forEach>
  <c:forEach var="dataValue" items=${data}>     
    <tr>
      <td>
             ${dataValue}       
     </td>
    </tr>
  </c:forEach>

Hope that helps 希望能有所帮助

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

相关问题 “ model.addAttribute”从控制器发送到JSP的数据类型是什么? - What is the type of data which, is sent by `model.addAttribute` from controller to JSP spring mvc use model.addAttribute(nav)无法使用$ {nav.id}获取jsp中父类的属性 - spring mvc use model.addAttribute(nav) can not get the parent class’s attributes in jsp with ${nav.id} Model.addAttribute 不向 .jsp 传递任何东西 - Model.addAttribute not passing any thing to the .jsp Spring Boot model.addAttribute - 如何让消息正确显示? - Spring Boot model.addAttribute - how to get message to display correctly? model.addAttribute()参数 - model.addAttribute() parameters Spring MVC:在Controller中使用model.addAttribute()将各种列表发送到Ajax方法 - Spring MVC : Using model.addAttribute() in Controller to send various Lists to Ajax method 春天的@ModelAttribute,model.addAttribute有什么区别? - What is the difference between @ModelAttribute, model.addAttribute in spring? model.addAttribute() 对于每个循环 - model.addAttribute() For Each Loop 如何在Spring MVC中的JSP中为JSP提供数据模型 - How to provide data model for JSP within a JSP in spring mvc 如何使用 Model.addAttribute() 添加的变量作为参数传递给 thymeleaf 的 hasRole 方法? - How can I use the variables that added by Model.addAttribute() to pass as a parameter in hasRole method of thymeleaf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM