用 MultiActionController 实现 SimpleFormController的部分功能?

[b]Controller[/b]
[code] 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;
}[/code]

[code] 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);
}[/code]

[b]FormBean[/b]
[code]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;
}
}[/code]

[b]Validator[/b]
[code]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");
}
}[/code]

[b]JSP[/b]
[code]<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>[/code]
刚刚做好一个测试的东东,打算在项目中这样使用,有什么问题请大家批评指正,谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值