2014년 2월 4일 화요일

Spring MVC And List Example

In this tutorial, we show you how to print the List values via JSTL c:forEach tag. P.S This web project is using Spring MVC frameworks v3.2 1. Project Structure Review the project directory structure, a standard Maven project. 이미지 2. Project Dependencies Add Spring and JSTL libraries. pom.xml

  3.2.2.RELEASE
  1.2
 

 

  
  
   jstl
   jstl
   ${jstl.version}
  

  
  
   org.springframework
   spring-core
   ${spring.version}
  

  
   org.springframework
   spring-web
   ${spring.version}
  

  
   org.springframework
   spring-webmvc
   ${spring.version}
  

 
3. Spring Controller A Spring controller to return a List.
/*MainController.java*/

package com.mkyong.web.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {

 @RequestMapping(value = "/", method = RequestMethod.GET)
 public ModelAndView getdata() {

  List list = getList();

  //return back to index.jsp
  ModelAndView model = new ModelAndView("index");
  model.addObject("lists", list);

  return model;

 }

 private List getList() {

  List list = new ArrayList();
  list.add("List A");
  list.add("List B");
  list.add("List C");
  list.add("List D");
  list.add("List 1");
  list.add("List 2");
  list.add("List 3");

  return list;

 }

}
4. JSP Page To print the returned List from controller, uses JSTL c:forEach tag.
  • index.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


 

Spring MVC and List Example

  • ${listValue}

댓글 없음:

댓글 쓰기