简体   繁体   English

Spring MVC Web应用程序-正确使用模型

[英]Spring MVC Web Application - proper use of Model

I am developing Web Application in Java using Spring Framework. 我正在使用Spring Framework用Java开发Web应用程序。 On one page, I am letting user pick year. 在一页上,我让用户选择年份。 Here is the code: 这是代码:

@Controller
public class MyController {

    @RequestMapping(value = "/pick_year", method = RequestMethod.GET)
    public String pickYear(Model model) {
        model.addAttribute("yearModel", new YearModel);
        return "pick_year";
    }

    @RequestMapping(value = "/open_close_month_list", method = RequestMethod.POST)
    public String processYear(Model model, @ModelAttribute("yearModel") YearModel yearModel) {
        int year = yearModel.getYear();
        // Processing
    }
}


public class YearModel { 
    private int year;

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}

This implementation works, but I would like to use something simplier to get year from the user. 此实现有效,但我想使用更简单的方法从用户那里获得帮助。 I think making special model just to get one integer is not very good approach. 我认为仅仅为了得到一个整数而制作特殊模型并不是很好的方法。

So, my question is: Is it possible to somehow simplify this code? 因此,我的问题是:是否可以以某种方式简化此代码?

Thank you for any help. 感谢您的任何帮助。 Milan 米兰

Usually you use the model to pass data from controller to view, and you use @RequestParam to get data from a submitted form in a controller. 通常,您使用模型将数据从控制器传递到视图,并使用@RequestParam从控制器中的提交表单获取数据。 Meaning, your POST method would look like: 意思是,您的POST方法如下所示:

public String processYear(@RequestParam("year") int year) {
    // Processing
}

Indeed you just need to store the integer, you don't need to create a whole new special class to hold it 实际上,您只需要存储整数,就不需要创建一个全新的特殊类来保存它

@Controller
public class MyController {
    @RequestMapping(value = "/pick_year", method = RequestMethod.GET)
    public String pickYear(ModelMap model) {
        model.addAttribute("year", 2012);
        return "pick_year";
    }

    @RequestMapping(value = "/open_close_month_list", method = RequestMethod.POST)
    public String processYear(@ModelAttribute("year") int year) {
        // Processing
    }
}

What you could instead (if possible) is rework your view so that you can use @RequestParam to pass directly an integer to your method pickYear rendering it into the view so that this parameter could be passed onto the second method processYear in the same way. 相反,您可以(如果可能)对视图进行重做,以便可以使用@RequestParam将整数直接传递给方法pickYear将其呈现到视图中,以便可以以相同的方式将此参数传递给第二个方法processYear

@Controller
public class MyController {
    // In the pick_year view hold the model.year in any hidden form so that
    // it can get passed to the process year method
    @RequestMapping(value = "/pick_year", method = RequestMethod.GET)
    public ModelAndView pickYear(@RequestParam("year") int year) {
        ModelAndView model = new ModelAndView("pick_year");
        model.addAttribute("year", 2012);
        return model;
    }

    @RequestMapping(value = "/open_close_month_list", method = RequestMethod.POST)
    public String processYear(@RequestParam("year") int year) {
        // Processing
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM