在不同的控制器之间传递对象Spring Web MVC

时间:2013-11-18 15:57:11

标签: java spring-mvc controllers

我是Spring Web MVC Framework的绝对初学者,因此我将感谢每一个提案/帮助/建议。为此,我提前感谢你。

----------------------------------------------- --------------------------------------

基本上我正在处理表格。 一般来说,我的申请包括几个步骤:

  1. 登录表单(用户可以使用其用户名和密码登录)
  2. MyForm (用户完成并将表单发送到数据库)
  3. 很简单。但这里有一件重要的事情。我如何显示“MyForm”? 当用户键入其用户名和密码时,应用程序将进入数据库并检查用户用户名是否已完成表单。根据这个条件,我使用适当的数据库列映射/填充MyForm对象属性(带有setter)。

    显然,我可以说两个不同的MyForm对象 - 一个用户是新用户,另一个用户已经知道用户。这就是诀窍。 我必须将MyForm对象传递给负责显示整个事物的MyFormController。 Ok View负责显示,但你明白我的观点。

    ----------------------------------------------- --------------------------------------

    LoginController.java


    package si.src.controllers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import si.src.forms.Login;
    import si.src.forms.Obrazec;
    import si.src.services.LoginService;
    import si.src.services.ObrazecService;
    import si.src.validators.LoginValidator;
    
    @Controller
    public class LoginController{
    
        private LoginService loginService;
        private ObrazecService obrazecService;
    
        private static Log log = LogFactory.getLog(LoginController.class);
    
        /**
         * Creates an object Login in which user information
         * (user name and password) is stored.
         * @return ModelAndView see "login.jsp" for view
         */
        @RequestMapping(value="/login", method=RequestMethod.GET)
        public ModelAndView login(){
            //System.out.println("Bla");
            Login userLogin=new Login();
            return new ModelAndView("login", "loginForm", userLogin);
        }
    
        /**
         * Data binding between form with modelAttribute="loginForm" (see "login.jsp") 
         * and object Login. In addition user information validation is performed.
         * @param login contains user information i.e. user name and password
         * @param error represent the validation results
         * @return view to render
         */
        @RequestMapping(value="/login", method=RequestMethod.POST)
        public String signIn(@ModelAttribute("loginForm") Login login,  BindingResult error){   
    
    
            LoginValidator validator = new LoginValidator();
            validator.validate(login, error);
    
            //returns string
            String res = loginService.login(login);
            System.out.println("You shall not pass: " + res);
            //System.out.println(login.getUsername());
            //System.out.println(login.getPassword());
    
            if(error.hasErrors()){
                return "login";
            }else{
                Obrazec obr = obrazecService.getObrazec();
                System.out.println(obr.getPrviDelSekStiriDrugo());
                //resolves to /WEB-INF/jsp/successfulLogin.jsp
                return "successfulLogin";
            }
    
        }
    
        @RequestMapping(value="/logout", method=RequestMethod.GET)
        public ModelAndView logOut(){           
            //System.out.println("Win");
            Login userLogin=new Login();
            return new ModelAndView("login", "loginForm", userLogin);
        }
    
        /**
         * Injected by Spring
         * @param loginService
         */
        public void setLoginService(LoginService loginService) {
            this.loginService = loginService;
        }
    
        /**
         * Injected by Spring
         * @param obrazecService
         */
        public void setObrazecService(ObrazecService obrazecService) {
            this.obrazecService = obrazecService;
        }
    }
    

    ObrazecController.java


    package si.src.controllers;
    
    import java.util.Iterator;
    import java.util.List;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import si.src.forms.Obrazec;
    import si.src.services.ObrazecService;
    
    @Controller
    public class ObrazecController {
    
        private ObrazecService obrazecService;  
    
        private static Log log = LogFactory.getLog(ObrazecController.class);
    
        /**
         * Creates an object Obrazec which presents a specific
         * form with its fields.
         * @return ModelAndView see("obrazec.jsp") for view
         */
        @RequestMapping(value="/obrazec", method=RequestMethod.GET)
        public ModelAndView obtainTheFrom(){
            Obrazec obrazec = new Obrazec();
            return new ModelAndView("obrazec", "obrazecForm", obrazec);
        }
    
        @RequestMapping(value="/obrazec", method=RequestMethod.POST, params="save")
        public String submitTheFrom(@ModelAttribute("obrazecForm") Obrazec obrazec, BindingResult errors){      
    
            String ipAddress="IPv4";
            String oddan="D";
    
            // Test 1 - OK (check the DB)
            //obrazecService.sendForm(obrazec, ipAddress, oddan);
    
            // Test 2 - OK (check the DB)
            // obrazecService.sendZavezanci(obrazec.getSeznamZavezancev());
    
            // Test 3 - OK (check the DB)
            //obrazecService.submitForm(obrazec);
    
            // Test 4 -  
            //Obrazec o = obrazecService.getObrazec();
            //System.out.println(o.getNazivOrgana());
    
            // Test 5
            /*
            List<String> seznamZavezancev = obrazecService.getSeznamZavezancev();   
    
            for (String s : seznamZavezancev){
                System.out.println(s);
            }*/
    
            //resolves to /WEB-INF/jsp/win.jsp
            return "win";
        }
    
        /**
         * Injected by Spring
         * @param loginService
         */
        public void setObrazecService(ObrazecService obrazecService) {
            this.obrazecService = obrazecService;
        }
    }
    

    有没有办法将一个对象从LoginController.java传递给ObrazecController.java。如果有不同的方法来处理这种情况,我们将非常感激!

    谢谢!

0 个答案:

没有答案