论坛首页 入门技术论坛

用 MultiActionController 实现 SimpleFormController的部分功能?

浏览 4916 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-12-15  
Controller
	protected BindException bindObject(HttpServletRequest request,
			Object command, Validator validator) throws Exception {
		ServletRequestDataBinder binder = createBinder(request, command);
		binder.bind(request);
		BindException errors = new BindException(command,
				getCommandName(command));
		if (validator.supports(command.getClass())) {
			ValidationUtils.invokeValidator(validator, command, errors);
		}
		return errors;
	}


			TestFormBean formBean = new TestFormBean();
			Collection testList = new ArrayList();
			
			TestSelect a = new TestSelect();
			a.setCode("1");
			a.setName("a");

			TestSelect b = new TestSelect();
			b.setCode("2");
			b.setName("b");
			
			TestSelect c = new TestSelect();
			c.setCode("3");
			c.setName("c");		
			
			testList.add(a);
			testList.add(b);
			testList.add(c);	


			BindException errorModule = bindObject(request, formBean,
					new TestValidator());
			if (errorModule.hasErrors()) {
				Map model = errorModule.getModel();
				model.put("testList", testList);
				return new ModelAndView("loginview", model);
			}


FormBean
public class TestFormBean {
	private String username;
	private String password;
	private Integer tokennumber;
	private String testSelect;
	private boolean testCheckBox;
	private String testRadio;
	private String testTextArea;
	private String testHidden;
	public boolean getTestCheckBox() {
		return testCheckBox;
	}
	public void setTestCheckBox(boolean testCheckBox) {
		this.testCheckBox = testCheckBox;
	}

	public String getTestHidden() {
		return testHidden;
	}

	public void setTestHidden(String testHidden) {
		this.testHidden = testHidden;
	}

	public String getTestRadio() {
		return testRadio;
	}

	public void setTestRadio(String testRadio) {
		this.testRadio = testRadio;
	}

	public String getTestTextArea() {
		return testTextArea;
	}

	public void setTestTextArea(String testTextArea) {
		this.testTextArea = testTextArea;
	}


	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getTokennumber() {
		return tokennumber;
	}
	public void setTokennumber(Integer tokennumber) {
		this.tokennumber = tokennumber;
	}

	public String testSelect() {
		return testSelect;
	}

	public void setTestSelect(String testSelect) {
		this.testSelect = testSelect;
	}

	public String getTestSelect() {
		return testSelect;
	}
}


Validator
public class TestValidator implements Validator {
	public boolean supports(Class clazz) {
		return TestFormBean.class.equals(clazz);
	}

	public void validate(Object obj, Errors errors) {
		TestFormBean fromBean = (TestFormBean) obj;
		Integer tokennumber = fromBean.getTokennumber();
		if (null == tokennumber || tokennumber.intValue() <= 1000)
			errors.rejectValue("tokennumber", "test", null, "must > 1000");
	}
}


JSP
<form:form commandName="command" name="cmForm" action="/cm/loginproc.htm">
           <form:input path="username"/>
	  <spring:bind path="command.password">
	   <input name="password" value='<c:out value="${status.value}"/>'>
	   <c:out value="${status.errorMessage}"/>
	  </spring:bind> 
	  <spring:bind path="command.tokennumber">
	   <input name="tokennumber" value='<c:out value="${status.value}"/>'>
	   <c:out value="${status.errorMessage}"/>
	  </spring:bind>  
        <form:select path="testSelect">
            <form:option value="-" label="--Please Select"/>
            <form:options items="${testList}" itemValue="code" itemLabel="name"/>
        </form:select>
        <form:checkbox path="testCheckBox"/>
	<form:radiobutton path="testRadio" value="1"/>
	<form:radiobutton path="testRadio" value="2"/>
	<form:textarea path="testTextArea" rows="3" cols="20"/>
	<form:hidden path="testHidden"/>
   </form:form>

刚刚做好一个测试的东东,打算在项目中这样使用,有什么问题请大家批评指正,谢谢!
   发表时间:2007-03-06  
能否把第二段代码的方法名及其参数命名 写一下。我没成功。
0 请登录后投票
   发表时间:2007-03-06  
我在使用 MultiActionController 现在遇到一个这样的问题, 比如有一个 addbook.jsp ,它是添加界面,同时也是数据提交后经校验不合格重新返回的界面(此时要显示错误信息)。

问题就是 addbook.jsp中有这样的代码 :

<spring:bind path="command.*">
    <c:if test="${status.error}">
        <font color="red">
            错误 :<br>
            <c:forEach items="${status.errorMessages}" var="error">
                <c:out value="${error}"/><br>
            </c:forEach>
        </font><br>
    </c:if>
</spring:bind>


现在问题就是 即使是第一次准备添加时 也会显示错误信息。我controller中代码是这样 :
        //数据校验
        BindException errors = super.bindObject2(req, book, new BookValidator());
        if (errors.hasErrors()) {
            return new ModelAndView(this.getAddbook(),errors.getModel());
        }


现在一种办法是在 第一次添加时 ,不运行上面代码,但会报
org.apache.jasper.JasperException: Neither Errors instance nor plain target object for bean name 'command' available as request attribute
	at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
	at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:112)
	at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:249)
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1063)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:827)


不知道这个问题有人遇到没有? 郁闷中。
0 请登录后投票
   发表时间:2007-03-07  
关键是你要先通过controller转到你的jsp,并且在controller中填写类似如下代码:

Map module = new HashMap();
module.put("command",new FormBean());
return new ModuleAndView(viewName,module);

0 请登录后投票
   发表时间:2007-03-08  
非常非常感谢。按你的方法成功了。谢谢
1 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics