论坛首页 Java企业应用论坛

讨论一下spring自己的MVC框架

浏览 27239 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-07-13  
这里好像没人提到spring自带的MVC框架,我最近研究了一下,感觉很不错。
spring支持多种类型的view,包括jsp、xslt、velocity、pdf、excel等技术。

spring提供多种控制器,其中有简单的对应一个请求的Controller,对应多个请求的MultiActionController,支持form表单和错误绑定功能的SimpleFormController等等,支持url路径重写(你可以以各种后缀名来命名)。

个人感觉非常灵活,有多种控制器可用,struts太死板难用,tapstry复杂难度高

另外从中学到了很多东西,jsp的作用就是实现视图,试图的功能其实很单一的,用来展现数据。

推荐大家研究研究,有什么优缺点大家讨论一下
   发表时间:2004-07-13  
看了一段时间,感觉如果全新的项目用它还是很不错的。
我觉得它的最大优点是对View的支持很灵活,可使用多种View技术,spring自己只做了一点包装工作
C和M倒没什么花头了,不过我倒认为这样的设计很合理
0 请登录后投票
   发表时间:2004-07-13  
没错,不过它自己的M也是很有特点的
在SimpleFormController中

jsp页面
<spring:hasBindErrors name="level">
   <tr><td align="center">
    <span class="errorMessageCss">发生以下错误</span>
	</td></tr>
</spring:hasBindErrors>
<spring:bind path="level.id">
   <tr><td align="center">
    <span class="errorMessageCss">${status.errorMessage}</span>
	</td></tr>
</spring:bind> 
<c:if test="${!empty message}">
   <tr><td align="center">
    <span class="errorMessageCss">${message}</span>
	</td></tr>
</c:if> 
</table>	
<table width="80%" align="center" border="0" cellpadding="0" cellspacing="1">
  <form name="form1" method="post" action="updatelevel.oant">
  <input type="hidden" name="action" value="<c:url value="${action}"/>"/>
  <tr> 
    <td width="30%" align="center">级别名称</td>
    <td width="40%" align="left">
       <spring:bind path="level.name">
          <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
       </td><td align="center" width="30%"><span class="errorMessageCss">${status.errorMessage}</span>
       </spring:bind> 
    </td>
  </tr>
  <tr> 
    <td width="30%" align="center">描述</td>
    <td width="40%" align="left">
       <spring:bind path="level.descript">
          <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
       </td><td align="center" width="30%"><span class="errorMessageCss">${status.errorMessage}</span>
       </spring:bind> 
    </td>
  </tr>
  <tr>
    <td width="30%" align="center">
	 <c:choose>
	  <c:when test="${action == 'update'}">
        <input type="submit" name="Submit" value="修改">
	  </c:when>
	  <c:when test="${action == 'add'}">
        <input type="submit" name="Submit" value="增加">
	  </c:when>	
	  </c:choose>
    </td>
    <td width="40%" align="left"><input type="reset" name="Submit2" value="返回" onclick="javascript:window.location='levelmanager.oant';"></td>
	<td width="30%">&</td>
  </tr>
  <tr>
    <td></td><td>
       <spring:bind path="level.id">
          <input type="hidden" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
       </spring:bind>     
     </td>
  </tr>     
  </form>  
</table>


控制器

public class LevelFormController extends SimpleFormController {
	private LevelService levelService;

	public LevelFormController() {
		setFormView("updatelevel");
	}
	
	protected Object formBackingObject(HttpServletRequest request)
		throws Exception {
		Level level = null;
		request.setAttribute("action",request.getParameter("action"));
		String id = request.getParameter("id");
		if (id != null && !"".equals(id)) {
			level =
				this.levelService.findById(Integer.parseInt(id));
			if (level == null) {
				level = new Level();
			}
		} else {
			level = new Level();
		}
		return level;
	}	

	protected ModelAndView processFormSubmission(
		HttpServletRequest request,
		HttpServletResponse response,
		Object command,
		BindException errors)
		throws Exception {
			Level level = (Level) command;
		String action = request.getParameter("action");
		if(errors.getErrorCount()>0){
			return super.processFormSubmission(request, response, command, errors);	
		}
		if (action == null || "".endsWith(action.trim())) {
			errors.rejectValue("id", "ID_REQUIRED", "参数错误");
		} else if ("update".equals(action.trim())) {
			try {
				this.levelService.update(level);
				request.setAttribute("message", "修改成功");
			} catch (ServiceException ex) {
				errors.rejectValue("id", "ID_REQUIRED", ex.getMessage());
			}
		} else if ("add".equals(action.trim())) {
			try {
				this.levelService.insert(level);
				request.setAttribute("message", "添加成功");
			} catch (ServiceException ex) {
				errors.rejectValue("id", "ID_REQUIRED", ex.getMessage());
			}
		} else {
			errors.rejectValue("id", "ID_REQUIRED", "参数错误");
		}
		return super.processFormSubmission(request, response, command, errors);			
		}
	
	protected ModelAndView onSubmit(
		HttpServletRequest request,
		HttpServletResponse response,
		Object command,
		BindException errors)
		throws Exception {
		request.setAttribute("address","levelmanager.oant");	
		return super.onSubmit(request, response, command, errors);
	}	
	/**
	 * @param service
	 */
	public void setLevelService(LevelService service) {
		levelService = service;
	}

}


这样感觉很好,如果要是开发工具能够可视化支持的话,也能做到RAD了
0 请登录后投票
   发表时间:2004-07-13  
上面打错了,应该是C
0 请登录后投票
   发表时间:2004-07-13  
它的M也是可以自动转换到VO上,并且写一个实现了Validator的VO检验类,然后通过配置,叶面就可以自动调用这个进行校验
0 请登录后投票
   发表时间:2004-07-13  
baichenhong 写道
如果要是开发工具能够可视化支持的话,也能做到RAD了

我觉得还是不要随意做这种假设为好,容易在心理上误导自己和别人.
0 请登录后投票
   发表时间:2004-07-13  
jxb8901 写道
baichenhong 写道
如果要是开发工具能够可视化支持的话,也能做到RAD了

我觉得还是不要随意做这种假设为好,容易在心理上误导自己和别人.


我是指对常用的RAD而言,不过这个通常指的就是微软,难道现在java在web开发上某个框架有什么好用的RAD开发工具吗?我对此不了解,请告知一下。
0 请登录后投票
   发表时间:2004-07-13  
baichenhong 写道
jxb8901 写道
baichenhong 写道
如果要是开发工具能够可视化支持的话,也能做到RAD了

我觉得还是不要随意做这种假设为好,容易在心理上误导自己和别人.


我是指对常用的RAD而言,不过这个通常指的就是微软,难道现在java在web开发上某个框架有什么好用的RAD开发工具吗?我对此不了解,请告知一下。

我的意思是既然现在还没有这样的可视化工具,以后也不见得有,就不要说"如果... 就..." , 这种假设不是没任何意义吗? 随便说一下,不要深究!
0 请登录后投票
   发表时间:2004-07-22  
哦,天哪,建议不要用spring mvc到项目中,你会发现她很复杂,甚至不比tapestry简单,
0 请登录后投票
   发表时间:2004-07-22  
spring的MVC很复杂?能说说你们是怎么用的吗?
0 请登录后投票
论坛首页 Java企业应用版

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