使用MultiActionController

时间:2010-08-25 10:41:06

标签: java spring spring-mvc

目前我在我的页面上使用分页,它使用MultiActionController完美地显示jsp页面,在同一页面上我现在要验证一个简单的文本字段(input/form:input)也想要检索名称和单击链接后,从下拉列表(Select option)中输入ID。简单!!

两个问题

  1. 我可以使用类实现Validator吗?并在配置或控制器内的其他方式注入与simpleformcontroller相同的方式?怎么样?请举例?

  2. 我可以在jsp中使用java bean - >我总是得到绑定错误,如何指示控制器使用这个bean?我已经作为参数传递给我的方法添加,并尝试重写newCommandObject

  3. Controller.java

    public ModelAndView add(HttpServletRequest request, HttpServletResponse response,  Person person) throws Exception { 
    
             return new ModelAndView("userpage"); 
        } 
    
    @Override 
        protected Object newCommandObject(Class clazz) 
        throws Exception {
    
             return new Person();
    }
    

1 个答案:

答案 0 :(得分:0)

我将在Spring版本中执行以下操作> 2.5

@Controller
public class YourController
{
    protected final Log logger = LogFactory.getLog(getClass());

    private final String yourInputJsp = "yourInputJsp";

    private final String yourInputJspSuccess = "yourInputJspSuccess";

    private YourService yourService;

    @Autowired
    @Qualifier("yourFormValidator")
    private YourFormValidator validator;

    @RequestMapping(value = "/yourRequest.htm", method = RequestMethod.GET)
    public String referenceData(ModelMap model, HttpServletRequest request) throws Exception 
    {
        yourService = new YourServiceImpl(ContextHandler.getWebAppContext(request));
        YourFormData yourFormData = new YourFormData();

        model.addAttribute("yourFormData", yourFormData);

        return yourInputJsp;
    }

    @InitBinder()
    public void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
    }

        @RequestMapping(value="/yourRequest.htm", method = RequestMethod.POST)
        public String process(@ModelAttribute("yourFormData") YourFormData yourFormData, BindingResult result, SessionStatus status, HttpServletRequest request)
        {
            String mav = yourInputJsp;
            validator.validate(yourFormData, result);

            if(!result.hasErrors())
            {
                //Some business logic

                mav = "redirect:yourInputJspSuccess.htm";
                status.setComplete();
            }

            return mav;
    }
}