论坛首页 Java企业应用论坛

Spring MutiActionController实现FormController在Controller中的代码事例

浏览 3153 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-02-12  
		public ModelAndView handelXqResourcePublish(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		Map model = new HashMap();
		try {
			//绑定
			XqZyfbFormBean formBean = new XqZyfbFormBean();
			BindException errorModule = bindObject(request, formBean,
					new XqzyfbValidator());
			if (errorModule.hasErrors())
				formBean.setOperation(XqZyfbFormBean._VIEW);
			
			//取得绑定后模型
			model = errorModule.getModel();
			
			//插入或者更新
			if (XqZyfbFormBean._UPDATE.equals(formBean.getOperation())) {
				XqZyfbModel pojo = new XqZyfbModel();
				pojo.setPkid(formBean.getPkid());
				pojo.setCustname(formBean.getCustname());
				pojo.setPtid(formBean.getPtid());
				pojo.setProdid(formBean.getProdid());
				pojo.setPcname(formBean.getPcname());
				pojo.setLen(formBean.getLen());
				pojo.setSl(formBean.getSl());
				pojo.setDj(formBean.getDj());
				pojo.setJhadr(formBean.getJhadr());
				pojo.setJhdt(formBean.getJhdt());
				pojo.setNote(formBean.getNote());
				pojo.setCmdeptid(new Integer(getUser(request).getCmdeptid()));
				pojo.setLinkmanid(getUser(request).getLinkmanid());
				generalService.saveXqResourcePublish(pojo);
				model.put("errMsg", "操作成功!");
				model.put("url", "/cm/resource/stockXqResource.do?ptid="
						+ formBean.getParamptid() + "&prodid="
						+ formBean.getParamprodid() + "&cmdeptid="
						+ formBean.getParamcmdeptid());
				return new ModelAndView("openerlocation", model);				
			}
			
			//修改取数据
			if (null!=formBean.getPkid()&&formBean.getPkid().intValue()>0)
			{
				XqZyfbModel pojo = generalService.selectXqResourceDetail(formBean.getPkid());
				formBean.setPkid(pojo.getPkid());
				formBean.setCustname(pojo.getCustname());
				formBean.setPtid(pojo.getPtid());
				formBean.setProdid(pojo.getProdid());
				formBean.setPcname(pojo.getPcname());
				formBean.setLen(pojo.getLen());
				formBean.setSl(pojo.getSl());
				formBean.setDj(pojo.getDj());
				formBean.setJhadr(pojo.getJhadr());
				formBean.setJhdt(pojo.getJhdt());
				formBean.setNote(pojo.getNote());				
			}
			
			//新增准备数据
			model.put("prodtypes", generalService.selectProductType());
			model.put("products", generalService.selectProduct());
			model.put("selectproducts", generalService.selectProduct());
		} catch (Exception e) {
			log.error("", e);
			return new ModelAndView("error");
		}
		return new ModelAndView("index_XqResourcePublish", model);
	}	


public class ResourceItemValidator extends AbstractValidator {
	public void validate(Object obj, Errors errors) {
		ResourceItemFormbean formBean = (ResourceItemFormbean) obj;
		if (null == formBean.getBatid() || formBean.getBatid().intValue() <= 0) {
			errors.rejectValue("batid", "batid.required", null, "default message!");
		}
		if (ResourceItemFormbean._SUBMIT.equals(formBean.getAction())) {

			if (null == formBean.getCustid()
					|| formBean.getCustid().intValue() <= 0) {
				errors
						.rejectValue("custid", "custid.required", null,
								"default message!");
			}
			if (null == formBean.getJs() || formBean.getJs().intValue() <= 0) {
				errors.rejectValue("js", "custid.required", null,
						"default message!");
			}
			if (null == formBean.getSl() || formBean.getSl().doubleValue() <= 0) {
				errors.rejectValue("sl", "custid.required", null,
						"default message!");
			}
			if (null == formBean.getDj() || formBean.getDj().intValue() <= 0) {
				errors.rejectValue("dj", "dj.required", null,
						"default message!");
			}
		}
	}
}


<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="/WEB-INF/c.tld" %>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<script language="javascript">
<!--
	window.onload=function(){
		form = document.cmForm;
		if (tper>0) form.sl.readOnly=true;
		var error = "";
		<spring:bind path="command.*">
		    <c:forEach items="${status.errorMessages}" var="errorMessage">
		          error=error+"<c:out value="${errorMessage}"/>\n";
		    </c:forEach>
		</spring:bind>
		if (""!=error)
		{
			alert(error);
		}				
	}
-->
</script>
<form:form commandName="command" name="cmForm" action="/cm/resource/resourceSale.do">
	<form:hidden path="batid"/>
	<input type="hidden" name="action" value="submit">
	<form:select path="custid" cssStyle="width:100%">  
		<form:option value="" label=""/>  
		<form:options items="${customer}" itemValue="custid" itemLabel="name"/>  
	</form:select>  	
	<form:input path="address" cssStyle="width:100%"/>
	<form:input path="js" cssStyle="width:100%" onblur="calculatSl();"/>
	<form:input path="sl" cssStyle="width:100%" onblur="calculatJe();"/>
	<form:input path="dj" cssStyle="width:100%" onblur="calculatJe();"/>
	<form:input path="je" cssStyle="width:100%" readonly="true"/>
	<form:textarea path="memo" cssStyle="width:100%" rows="2"/>
	<input type="button" name="btnOK" class="cbutton" onclick="doSubmit();" value="确定">
	<input type="button" name="btnCancle" onclick="window.close();" class="cbutton" value="取消">
</form:form>
   发表时间:2007-02-13  
if (XqZyfbFormBean._UPDATE.equals(formBean.getOperation())) {   
        XqZyfbModel pojo = new XqZyfbModel();   
        pojo.setPkid(formBean.getPkid());   
        pojo.setCustname(formBean.getCustname());   
        pojo.setPtid(formBean.getPtid());   
        pojo.setProdid(formBean.getProdid());   
        pojo.setPcname(formBean.getPcname());   
        pojo.setLen(formBean.getLen());   
        pojo.setSl(formBean.getSl());   
        pojo.setDj(formBean.getDj());   
        pojo.setJhadr(formBean.getJhadr());   
        pojo.setJhdt(formBean.getJhdt());   
        pojo.setNote(formBean.getNote());   
        pojo.setCmdeptid(new Integer(getUser(request).getCmdeptid()));   
        pojo.setLinkmanid(getUser(request).getLinkmanid());   
        generalService.saveXqResourcePublish(pojo);   
        return new ModelAndView("success", model);                  
    }   
       
    //显示明细   
    if (null!=formBean.getPkid()&&formBean.getPkid().intValue()>0)   
    {   
        XqZyfbModel pojo = generalService.selectXqResourceDetail(formBean.getPkid());   
        formBean.setPkid(pojo.getPkid());   
        formBean.setCustname(pojo.getCustname());   
        formBean.setPtid(pojo.getPtid());   
        formBean.setProdid(pojo.getProdid());   
        formBean.setPcname(pojo.getPcname());   
        formBean.setLen(pojo.getLen());   
        formBean.setSl(pojo.getSl());   
        formBean.setDj(pojo.getDj());   
        formBean.setJhadr(pojo.getJhadr());   
        formBean.setJhdt(pojo.getJhdt());   
        formBean.setNote(pojo.getNote());                  
    }   


对于如上代码,formBean中的属性与页面的属性一一对应,pojo与数据库table表中的字段一一对应,这样设计后却出现上面的干嘎代码,因为formBean中的大多数属性和pojo的大多数属性是相同的,但是有一些不同!能够有更简单的办法避免上面的尴尬的代码出现呢?本来想考虑使用反射,但是这么频繁的使用会否带来性能问题.

0 请登录后投票
论坛首页 Java企业应用版

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