天天看點

bboss mvc基礎配置介紹

bboss mvc基礎配置介紹,本文重點介紹bboss-mvc.xml檔案中的一些有意義的配置以及其什麼時候被加載。

1.bboss-mvc.xml加載

首先介紹bboss-mvc.xml檔案什麼時候會被加載,先談一下web.xml中bboss mvc的請求處理servlet的配置:

<servlet>
		<servlet-name>mvcdispather</servlet-name>
		<servlet-class>org.frameworkset.web.servlet.DispatchServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!--如果有多個目錄需要加載,請用,号分隔-->
			<param-value/WEB-INF/conf/bboss-*.xml</param-value>
		</init-param>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvcdispather</servlet-name>
		<url-pattern>*.page</url-pattern>
	</servlet-mapping>
	      

其中的/WEB-INF/conf/bboss-*.xml很關鍵,他會掃描/WEB-INF/conf下所有以bboss-開頭的mvc xml配置檔案并加載之;這裡可以配置多個目錄,用逗号分隔,隻要bboss-mvc.xml放在其中的一個目錄下就會在應用啟動的時候被加載。

2.bboss-mvc.xml中經常會用到的配置

2.1 檔案上傳插件配置

<property name="multipartResolver"   f:encoding="UTF-8"
         class="org.frameworkset.web.multipart.commons.CommonsMultipartResolver"/>          

常用的配置屬性有:

encoding,一般被配置為utf-8,用來對附件上傳表單中的請求參數進行單獨的編碼轉換,避免中文亂碼問題。

maxUploadSize:允許上傳的最大檔案大小,機關為byte,為-1時不限制大小。

maxInMemorySize:允許在記憶體中存放的最大檔案塊大小,以byte為機關,預設為10K

uploadTempDir:指定上傳檔案存放的臨時目錄,預設為應用臨時目錄

2.2 檔案下載下傳、json響應、字元串響應插件配置

<property name="httpMessageConverters">
     	<list>
     		<property class="org.frameworkset.http.converter.json.MappingJacksonHttpMessageConverter"/>
     		<property class="org.frameworkset.http.converter.StringHttpMessageConverter"/>
     		<property class="org.frameworkset.http.converter.FileMessageConvertor"/>
     	</list>        
     </property>      

如果不指定"org.frameworkset.http.converter.json.MappingJacksonHttpMessageConverter,那麼控制方法中以下寫法将不能正常工作:

public @ResponseBody(datatype="json") GouWuChe datagrid_data()      

也就是說不能正确地将對象GouWuChe 轉換為json對象傳回給請求用戶端。

如果不指定"org.frameworkset.http.converter.FileMessageConvertor,那麼控制器方法的以下寫法将不能正常工作:

public @ResponseBody File downloadFileFromFile(@RequestParam(name = "fileid") String fileid)
			throws Exception      

也就是說不能正常地下載下傳傳回的File對應的檔案。

如果不指定"org.frameworkset.http.converter.StringHttpMessageConverter,那麼控制器方法的以下寫法将不能正常工作:

public @ResponseBody(charset = "GBK")
	String sayHelloEnum(@RequestParam(name = "sex") SexType type)      

也就是說不能正常地将傳回值String響應給用戶端。

對于ajax請求響應出現的中文亂碼問題,解決辦法有兩個,一個是直接在StringHttpMessageConverter上通過responseCharset屬性全局指定響應字元編碼集,例如UTF-8或者GBK:

<property class="org.frameworkset.http.converter.StringHttpMessageConverter" f:responseCharset="UTF-8"/>      
<property class="org.frameworkset.http.converter.StringHttpMessageConverter" f:responseCharset="GBK"/>      

具體使用何種字元集取決于項目中采用的字元集。

另外一種解決辦法就是通過傳回參數注解ResponseBody的charset 屬性單獨對請求方法的響應字元串進行編碼,例如:

public @ResponseBody(charset = "UTF-8")
	String sayHelloEnum(@RequestParam(name = "sex") SexType type)      

2.3 全局攔截器配置(最常見的就是頁面保護機制配置)

攔截器配置在bboss-mvc.xml的org.frameworkset.web.servlet.gloabel.HandlerInterceptors節點中,所有的攔截器隻需要實作接口

org.frameworkset.web.servlet.HandlerInterceptor

接口提供了一下方法:

/**
	 * Intercept the execution of a handler. Called after HandlerMapping determined
	 * an appropriate handler object, but before HandlerAdapter invokes the handler.
	 * <p>DispatcherServlet processes a handler in an execution chain, consisting
	 * of any number of interceptors, with the handler itself at the end.
	 * With this method, each interceptor can decide to abort the execution chain,
	 * typically sending a HTTP error or writing a custom response.
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @param handler chosen handler to execute, for type and/or instance evaluation
	 * @return <code>true</code> if the execution chain should proceed with the
	 * next interceptor or the handler itself. Else, DispatcherServlet assumes
	 * that this interceptor has already dealt with the response itself.
	 * @throws Exception in case of errors
	 */
	boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMeta handlerMeta)
	    throws Exception;

	/**
	 * Intercept the execution of a handler. Called after HandlerAdapter actually
	 * invoked the handler, but before the DispatcherServlet renders the view.
	 * Can expose additional model objects to the view via the given ModelAndView.
	 * <p>DispatcherServlet processes a handler in an execution chain, consisting
	 * of any number of interceptors, with the handler itself at the end.
	 * With this method, each interceptor can post-process an execution,
	 * getting applied in inverse order of the execution chain.
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @param handler chosen handler to execute, for type and/or instance examination
	 * @param modelAndView the <code>ModelAndView</code> that the handler returned
	 * (can also be <code>null</code>)
	 * @throws Exception in case of errors
	 */
	void postHandle(
			HttpServletRequest request, HttpServletResponse response, HandlerMeta handlerMeta, ModelAndView modelAndView)
			throws Exception;

	/**
	 * Callback after completion of request processing, that is, after rendering
	 * the view. Will be called on any outcome of handler execution, thus allows
	 * for proper resource cleanup.
	 * <p>Note: Will only be called if this interceptor's <code>preHandle</code>
	 * method has successfully completed and returned <code>true</code>!
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @param handler chosen handler to execute, for type and/or instance examination
	 * @param ex exception thrown on handler execution, if any
	 * @throws Exception in case of errors
	 */
	void afterCompletion(
			HttpServletRequest request, HttpServletResponse response, HandlerMeta handlerMeta, Exception ex)
			throws Exception;
      

寫好監聽器,就可以進行配置了:

<!-- 配置全局控制器方法攔截器 -->
     <property name="org.frameworkset.web.servlet.gloabel.HandlerInterceptors" >
     	<list componentType="bean">
     		<property class="com.bboss.security.XXXXInterceptor"/>
     		<property class="com.bboss.security.SYSAuthenticateInterceptor">
     			<!-- 配置認證檢查攔截器攔截url模式規則 -->
     			<!-- <property name="patternsInclude">
     				<list componentType="string">
     					<property value="/**/*.page"/>
     				</list>
     			</property>-->
     			<!-- 配置認證檢查攔截器不攔截url模式規則 -->
     			<property name="patternsExclude">
     				<list componentType="string">
     					<property value="/uddi/queryservice/main.page"/>
     					<property value="/uddi/queryservice/verify.page"/>
     					<property value="/uddi/queryservice/requesterVerify.page"/>
     					<property value="/uddi/queryservice/detail.page"/>
     					<property value="/uddi/servicemanage/basic.page"/>    
     					<property value="/uddi/servicemanage/servicedesc.page"/>
     					<property value="/uddi/servicemanage/listmetadata.page"/>
     					<property value="/uddi/queryservice/visitpermission.page"/>
     					
     					<property value="/uddi/queryservice/logout.page"/>
     					
     					
     				</list>
     			</property>
     			<property name="redirecturl" value="/login.jsp"/>
     		</property>
     	</list>
     </property>      

2.4 url重寫規則配置

這個是用來配置所有控制器方法跳轉位址是否需要進行url重寫以及怎麼重寫的規則進行配置,以下就是最常用的配置:

<property name="viewResolver" class="org.frameworkset.web.servlet.view.InternalResourceViewResolver" singlable="true">
        <property name="viewClass" value="org.frameworkset.web.servlet.view.JstlView"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=""/>   </property>      

以上配置其實并沒有設定重寫規則,因為url重寫前置prefix被配置為"",重寫字尾suffix也被配置為"",這樣控制跳轉位址配置或者直接傳回的位址串就是實際的實體url位址,mvc架構不做任何處理。

除非有特殊要求的項目才會開啟url重寫規則,也就是配置url重寫字首prefix和重寫字尾suffix:

<property name="viewResolver" class="org.frameworkset.web.servlet.view.InternalResourceViewResolver" singlable="true">
        <property name="viewClass" value="org.frameworkset.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>   </property>      

url重寫前置prefix被配置為"/jsp/",重寫字尾suffix也被配置為".jsp",這樣控制跳轉位址配置或者直接傳回的位址串就是:"/jsp/"+returl+".jsp"。有人可能會擔心url重寫規則是否會影響性能,其實不會的,因為url隻會被計算一次,後面就從緩沖區中取已經重寫好的位址了。

以上就是bboss-mvc.xml中比較重要的一些應用可能會用到并修改的配置,其他的配置内容基本不用開發人員修改,保持預設配置即可。