I am not able to understant that only one bean object is created in spring mvc using dispatcher servlet or a new object gets created with every request?
Controller code:-
In the code i am setting the data in the LoginBean Object and setting it in the modelandview object in method abc.
Then in the jsp i am not entering any value for usename, in that case when i submit the form and when the handler method(initform) is called then i am trying to print the same lb.getusername which is not returing me any value.
Not able to understand the concept.
@Controller
public class LoginController{
ModelAndView mv=null;
EmployeeBean e=new EmployeeBean();
AutoBean autobean;
@Autowired
public LoginController(AutoBean autobean){
System.out.println("autobean");
this.autobean=autobean;
}
@RequestMapping(value="/login")
public ModelAndView abc(){
System.out.println("here");
System.out.println("here1");
LoginBean lb=new LoginBean();
lb.setUsename("ankita");//setting value
return new ModelAndView("login","loginbean",lb);
}
@RequestMapping(value="/abc1",method=RequestMethod.POST)
public ModelAndView initform(@ModelAttribute("loginbean")LoginBean lb,BindingResult result,Model model){
System.out.println("*****"+result.getErrorCount());
System.out.println("hello");
autobean.setName("yayme");
System.out.println(autobean.getName());
model.addAttribute("autobean", autobean);
System.out.println("username"+lb.getUsename());// query??
if(lb.getPassword().equals("ankita"))
/*{
mv=new ModelAndView();
e.setId("1001");
e.setName("ankita");
mv.addObject("employee", e);
mv.addObject("emp", new Emp());
mv.setViewName("success");
return mv;
}*/
return new ModelAndView("success","emp",new Emp());
else
return new ModelAndView("fail","lb1",lb);
}
login.jsp
<form:form action="abc1" commandName="loginbean">
username:<form:input path="usename" />
password:<form:password path="password"/>
<input type="submit"/>
</form:form>
Please suggest?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…