How does springMVC pass Model data from controller to jsp pages

  • 2020-08-22 22:06:13
  • OfStack

Store data in action as follows:


@Controller //  To join the IOC The container 
//@RequestMapping(value="/topic")
public class TopicAction {

  @Resource(name = "topicServiceImpl")
  private TopicService topicService;

  /**
   *  The homepage 
   */
  @RequestMapping(value="/index")
  public String index(Model model){
    List<Topic> topicList = topicService.getAllTopicList();
    model.addAttribute("topics", topicList);
    System.out.println(" encapsulated model="+model);
    return "index.jsp";
  }
}

How do You get the data in an jsp page?

The first thing to figure out is what type of data was passed? The data is then parsed and displayed.

The el expression ${topics} is used to get the data as follows:


[Topic [id=0, time=2016-12-05 08:29:02.0, title=html Baidu encyclopedia , contents=null, isTuiJian=0, good=15, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-05 08:29:04.0, title=JSP Introduction to the , contents=null, isTuiJian=0, good=2, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-05 08:28:59.0, title=test, contents=null, isTuiJian=0, good=3, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-05 08:28:52.0, title=test2, contents=null, isTuiJian=0, good=0, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-13 09:29:30.0, title= Classic songs , contents=null, isTuiJian=0, good=3, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-05 08:28:56.0, title=test4, contents=null, isTuiJian=0, good=0, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-05 09:02:19.0, title=tst2, contents=null, isTuiJian=0, good=30, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-08 14:45:47.0, title= My world , contents=null, isTuiJian=0, good=3, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-08 19:39:15.0, title= Your world , contents=null, isTuiJian=0, good=0, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-09 14:17:17.0, title=test, contents=null, isTuiJian=0, good=0, user=null, replyList=[], type=null], Topic [id=0, time=2016-12-09 14:19:21.0, title= I の , contents=null, isTuiJian=0, good=0, user=null, replyList=[], type=null]]

This is obviously not the data we're looking for,

Using this ${topics[1].title}, the data is as follows:

Here you can add an for loop outside ${topics[1].title} to output each piece of data.

Here is my personal conclusion.


Related articles: