`
sslaowan
  • 浏览: 373837 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring MVC(CoC)+FCKEditor

阅读更多

      设计思路参考昨天写的《关于RCP与Web交互的一些设计思路 》,使用了Spring MVC,特别使用了Spring对于CoC(Convention over Configuration,惯例优先原则)的支持,由于需要富文本编辑器,因此我选择了著名的FCKEditor。由于我不需要支持上传功能,因此没有用FCKEditor的Java包。

      虽然我使用的是Spring3,但是我没有使用其对REST的支持,也没有使用注解。

      所需要的其他Jar包包括:

      1 antlr-2.7.5H3

      2 antlr.jar

      3 bsh-1.3.0.jsr

      4 cglib-nodep-2.1_3.jar

      5 com.springsource.org.antlr-3.0.0.M2.jar

      6 asm-2.2.2.jar

      7 asm-commons-2.2.2.jar

      需要用这些包替换项目中已经有的其他antlr,cglib,asm包,否则就会报相应的错误。

      FCKEditor是2.6版。

     我的文件结构如下:

RestOnSpring

    -src

        -org.jport.samples.web.mvc

               -RichEditorController.java

   -WebRoot

       |-fckeditor

           +editor

            |-fckconfig.js

            |-fckeditor.js

            |-fckpackager.xml

            |-fckstyles.xml

            |-fcktemplates.xml

       |-WEB-INF

            |-jsp

                |-richeditor

                    |-create.jsp

                    |-display.jsp

            |-applicationContext.xml

            |-richeditor-servlet.xml

            |-web.xml

   在上篇文章里,我设计好了如下的URI:

   显示新增页面 richeditor/create

   保存  richeditor/save。对于是新增还是修改,需要借助Form中的一个隐藏域,<input type='hidden' id='id'  value='<%=id%>'/>,将id置于表述之中而不是URI中,传给Web服务器,这样做是因为表述包含了我要传递的数 据,将相当于RPC(远程过程调用)中的数据对象,然后借用Hibernate中saveOrUpdate的思想,如果该id==null则意味着新增, 否则意味着修改。

   显示阅读页面 richeditor/diplay?id=123

   显示修改页面 richeditor/edit?id=123

 

        RichEditorController.java如下

package org.jport.samples.web.mvc;

import java.lang.annotation.Annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * The central richeditor
 * {@link org.springframework.web.servlet.mvc.Controller} implementation for the
 * application.
 * 
 * @author sslaowan
 */
   
public class RichEditorController extends MultiActionController{

	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return new ModelAndView();
	}

	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = (String) request.getParameter("id");
		//通过调用业务服务,根据id返回要修改的内容
		String text=(String) request.getParameter("value");
		 
		ModelAndView mv = new ModelAndView("forward:/richeditor/create.htm");
		mv.addObject("id", id);
	    mv.addObject("value", text);
		return  mv;
	}
	
	public ModelAndView display(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
 
		String id=(String) request.getParameter("id");
		//通过调用业务服务,根据id返回要修改的内容
		String text=id;
		request.setAttribute("value", text);
		return new ModelAndView();
	}

	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String text = request.getParameter("FCKeditor1");
		String id=request.getParameter("id");
		if("".equals(id)){
			System.out.println("save:"+text);
		}else{
			System.out.println("update:"+text);
		}
		
	 	ModelAndView mv = new ModelAndView("forward:/richeditor/display.htm?id="+text);
	        mv.addObject("value", text);
		return mv;
	}

 

}

 

       richeditor-servlet.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<!-- maps request URLs to Controller names -->
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

	<!-- Controller names are not important when using the above HandlerMapping implementation-->
	<bean class="org.jport.samples.web.mvc.RichEditorController" />

	<!-- this bean with the well known name generates view names for us -->
	<!-- not strictly required since we just want to accept the defaults-->
	<bean id="viewNameTranslator"
		class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>


</beans>

        create.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%String value=(String)request.getAttribute("value")==null?"":(String)request.getAttribute("value"); %>
<%String id=(String)request.getAttribute("id")==null?"":(String)request.getAttribute("id"); %>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>FCKeditor - Sample</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	 	 
	<script type="text/javascript" src="../fckeditor/fckeditor.js"></script>
</head>
<body>
	 
	<form action="/RestOnSpring/richeditor/save.htm" method="post">
	   <input type="hidden" name='id' value='<%=id%>'/>

      	<script type="text/javascript">
<!--
 
var sBasePath = '/RestOnSpring/fckeditor/' ;

var oFCKeditor = new FCKeditor( 'FCKeditor1' ) ;
oFCKeditor.BasePath	= sBasePath ;
oFCKeditor.Height	= 300 ;
oFCKeditor.Value	= '<%=value%>' ;
oFCKeditor.Create() ;
//-->
		</script>
		<br />
		 
	</form>
</body>
</html>

    display.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%String value=(String)request.getAttribute("value")==null?"":(String)request.getAttribute("value"); %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    

  </head>
  
  <body>
   <div>
   <form action="/RestOnSpring/richeditor/edit.htm?id=<%=value%>" method="post">           
		 	<input type="submit" value="编辑" /> 
		 </form>
	</div>
	<hr />
    <%=value%>
  </body>
</html>

 

   为了单纯的测试Web层,我通过在页面间传递文本内容(通过mv.addObject("value",text);这句话与request.setAttribute("value",text);等价)来模拟保存的数据,用String text=id或者是打印语句的方式来模拟业务。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics