1、在MyEclipse下建立一個web應用
2、導包
從解壓后的 spring 文件夾中尋找 jstl.jar、standard.jar、spring.jar、spring-webmvc.jar、spring-webmvc-portlet.jar、commons-logging.jar拷貝到 WEB-INF/lib 目錄下。
3、編輯web.xml
在web.xml文件中添加以下代碼:
<!-- 設定Spring的根上下文 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 設定ViewRendererServlet --> <servlet> <servlet-name>ViewRendererServlet</servlet-name> <servlet-class> org.springframework.web.servlet.ViewRendererServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ViewRendererServlet</servlet-name> <url-pattern>/WEB-INF/servlet/view</url-pattern> </servlet-mapping>
<!-- 設定加載一個Portlet的Servlet, 該配置為Pluto所需--> <servlet> <servlet-name>SpringTestPortlet1</servlet-name> <servlet-class> org.apache.pluto.container.driver.PortletServlet </servlet-class> <init-param> <param-name>portlet-name</param-name> <param-value>SpringTestPortlet1</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringTestPortlet1</servlet-name> <url-pattern>/PlutoInvoker/SpringTestPortlet1</url-pattern> </servlet-mapping> <jsp-config> <taglib> <taglib-uri>http://portals.apache.org/pluto</taglib-uri> <taglib-location>/WEB-INF/tld/pluto.tld</taglib-location> </taglib> </jsp-config> |
4、編輯Portlet.xml文件
<?xml version="1.0" encoding="UTF-8"?> <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"> <portlet> <portlet-name>SpringTestPortlet1</portlet-name> <display-name>SpringTestPortlet1</display-name> <portlet-class> org.springframework.web.portlet.DispatcherPortlet </portlet-class> <init-param> <name>contextConfigLocation</name> <value>/WEB-INF/springtest-portlet1.xml</value> </init-param> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> <portlet-mode>edit</portlet-mode> <portlet-mode>help</portlet-mode> </supports> <portlet-info> <title>SpringTestPortlet1</title> </portlet-info> </portlet> </portlet-app> |
5、編寫相應的Java POJO類、jsp文件、Spring配置文件
Pojo類在此不再敷述,jsp文件中的開始標簽部分如下:
<%@ page import="javax.portlet.*" contentType="text/html; charset=utf-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <portlet:actionURL var="actionURL" /> |
在 WEB-INF 下新建 applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Default View Resolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- Default ExceptionHandler --> <bean id="defaultExceptionHandler" class="org.springframework.web.portlet.handler.SimpleMappingExceptionResolver"> <property name="order" value="10" /> <property name="defaultErrorView" value="error" /> <property name="exceptionMappings"> <props> <prop key="javax.portlet.UnavailableException"> unavailable </prop> <prop key="java.lang.Exception">error</prop> </props> </property> </bean> </beans>
|
該配置文件中,定義了兩個 Bean。其中第一個 Bean 定義了視圖的默認解析方式,使用 JSP 作為視圖(View),到 /WEB-INF/jsp/ 目錄下尋找 Jsp 文件,并且視圖名稱為 jsp 文件名的前綴。
在第二個 Bean 中,定義了異常處理方式。如果發生 javax.portlet.UnavailableException 異常,則呈現 unavailable 視圖,即 unavailable.jsp 文件;如果發生其它的 java.lang.Exception 異常,則呈現 error 視圖,即 error.jsp 文件。
在 WEB-INF 下新建 PortletSpringTestPortlet1 的 Spring 上下文配置文件 springtest-portlet1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.web.portlet.handler.PortletModeHandlerMapping"> <property name="portletModeMap"> <map> <entry key="view" value-ref="viewController" /> </map> </property> </bean> <bean id="viewController" class="org.springframework.web.portlet.mvc.SimpleFormController"> <property name="commandClass" value="springportal.command.AddressBook" /> <property name="commandName" value="addressBook" /> <property name="formView" value="addressInput" /> <property name="successView" value="result" /> </bean> </beans>
|
該上下文根據 Portlet 模式分配控制器,由 Bean 定義可知,僅僅定義了 view 模式的控制器,edit 和 help 模式都沒有進行定義。
在 View 模式的控制器 viewController 中,定義了 commandClass 和 commandName 來保存用戶輸入的表單數據,addressInput 視圖(addressInput.jsp)為輸入表單,result 視圖(result.jsp)呈現表單提交結果。
6、遇到的問題
java.lang.ClassCastException:org.springframework.web.servlet.support.JstlUtils$SpringLocalizationContext
I had
the same issue with Pluto current bundle distribution 1.1.6.
The ClassCastException is actually from Pluto's default theme jsp. It's
trying to cast a
org/springframework/web/servlet/support/JstlUtils$SpringLocalizationContext
into a java/lang/String . This is due to that the JSTL implementation doesn't
recognize SpringLocalizationContext as a LocalizationContext instance though
SpringLocalizationContext surely implements the interface. The root cause is
that two classes are loaded from different classloader thus the instanceof
check failed. |
解決方法:將pluto-2.0"webapps"pluto"WEB-INF"lib下的jstl.jar和standard.jar移動到pluto-2.0"lib目錄下
源碼下載地址:點擊下載
