how to pass variables from java to jsp in Spring

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

in a spring framework I want to pass some variables(objects) to jsp page. I can pass one object with:

 ModelAndView modelAndView= new ModelAndView("JspPageName", "message", message);   return message 

@Controller public class DomainEkleController {     private DomainJDBCTemplate  domainJDBCTemplate;     private MemurJDBCTemplate memurJDBCTemplate;     @ModelAttribute("Domain")     public Domain getDomain()     {         return new Domain();     }      @Autowired     @Qualifier("domainJDBCTemplate")     public void setDomainJDBCTemplate(DomainJDBCTemplate domainJDBCTemplate) {         this.domainJDBCTemplate = domainJDBCTemplate;     }      @Autowired     @Qualifier("memurJDBCTemplate")     public void setMemurJDBCTemplate(MemurJDBCTemplate memurJDBCTemplate) {         this.memurJDBCTemplate = memurJDBCTemplate;     }      @RequestMapping(value="/DomainEkle")     public ModelAndView domainEkle() {          List<Memur> memurlar=memurJDBCTemplate.getAll();         System.out.println(memurlar);         /*for(Memur x:memurlar)         {             System.out.println(x.getIsim());         }*/         String message = "Hello World, Spring 3.0!";         ModelAndView domain_ekle= new ModelAndView("DomainEkle", "message", message);         return domain_ekle;     }         @RequestMapping(value="/DomainEkle",method=RequestMethod.POST)     public ModelAndView domain_eklendi_fonksiyon(@ModelAttribute("Domain")Domain domain,   ModelMap model)     {             model.addAttribute("domain_adi", domain.getDomain_adi());         model.addAttribute("sunucu_no", domain.getSunucu_no());         model.addAttribute("tarih", domain.getTarih());         model.addAttribute("ilgili_memur_no",domain.getIlgili_memur_no());          String message="Domain Kaydi Yapilmistir!";         ModelAndView dm_eklendi=new ModelAndView("DomainEkle","message",message);          domainJDBCTemplate.addDomain(domain);         return dm_eklendi;        }  } 

回答1:

You will notice ModelAndView has a constructor that accepts a Map<String, ?>

ModelAndView mav = new ModelAndView("someView", map); 

So construct a map to hold the model attributes

 Map<String, Object> map = new HashMap<>();  map.put("attrib1", someAttrib); 

You can put as many key-value paired objects in this map and pass it to the constructor or call the

mav.addAllObjects(map); 

method to add all the attributes. These attributes will end up in the HttpServletRequest where they will be available to the JSP.

This ends up being equivalent to passing a Model or ModelMap as an argument to your handler methods. Same as you do in your domain_eklendi_fonksiyon() method.



回答2:

You can use

mav.addObject("data1", somdeData1); mav.addObject("data2", somdeData2); ... 

to add additional model attributes.

Note that you don't have to create the ModelAndView object yourself. You can add a Model parameter to your controller methods which is added by Spring when the method is called. You can simply use this model to add your data. After that you need only to return the view name.

@RequestMapping(value="/DomainEkle") public String domainEkle(Model model) {   model.addAtribute("data", data);   return "myViewName"; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!