Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass variables from java to jsp in Spring

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

But how can I send more than one objects from java to jsp. Actually I know that I can make a object array and send this array, however I wonder the best way of doşng this My code which send the datas to jsp is this:

@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;

      }

}
like image 684
ali insan Avatar asked Feb 16 '23 08:02

ali insan


2 Answers

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.

like image 172
Sotirios Delimanolis Avatar answered Feb 20 '23 10:02

Sotirios Delimanolis


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";
}
like image 22
micha Avatar answered Feb 20 '23 11:02

micha