我无法理解在 spring mvc 中使用调度程序 servlet 只创建了一个 bean 对象,或者每个请求都会创建一个新对象?
控制器代码:-
在代码中,我在 LoginBean 对象中设置数据并将其设置在方法 abc 中的模型和视图对象中。
然后在jsp中我没有为usename输入任何值,在这种情况下,当我提交表单并且调用处理程序方法(initform)时,我试图打印相同的lb.getusername,它不会返回任何值。无法理解概念。
@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);
}
登录.jsp
<form:form action="abc1" commandName="loginbean">
username:<form:input path="usename" />
password:<form:password path="password"/>
<input type="submit"/>
</form:form>
请建议?