如何在Spring中将变量从java传递到jsp [英] how to pass variables from java to jsp in Spring

查看:185
本文介绍了如何在Spring中将变量从java传递到jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在spring框架中,我想将一些变量(对象)传递给jsp页面.我可以通过以下方式传递一个对象:

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

但是如何将多个对象从Java发送到jsp.实际上我知道我可以创建一个对象数组并发送该数组,但是我想知道这样做的最佳方法 我将数据发送到jsp的代码是这样的:

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;

      }

}

推荐答案

您会注意到ModelAndView具有接受

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

方法添加所有属性.这些属性将最终出现在HttpServletRequest中,供JSP使用.

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

这最终等同于将ModelModelMap作为参数传递给处理程序方法.与您在domain_eklendi_fonksiyon()方法中所做的相同.

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.

这篇关于如何在Spring中将变量从java传递到jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆