Spring PortletMVC环境搭建

Spring PortletMVC环境搭建

1、MyEclipse下建立一个web应用

2、导包

从解压后的 spring 文件夹中寻找 jstl.jarstandard.jarspring.jarspring-webmvc.jarspring-webmvc-portlet.jarcommons-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>

   

    <!-- 设定加载一个PortletServlet, 该配置为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.

Solution: move the jstl-1.0.6.jar and standard-1.0.6.jar under pluto-1.1.6"webapps"pluto"WEB-INF"lib into pluto-1.1.6"shared"lib 

Note: do not include these 2 jar files in your portlet application (war file).

——引自http://forum.springsource.org/showthread.php?t=57816

解决方法:将pluto-2.0"webapps"pluto"WEB-INF"lib下的jstl.jarstandard.jar移动到pluto-2.0"lib目录下


源码下载地址:点击下载



唯美古典的工作室

你可能感兴趣的:(Spring PortletMVC环境搭建)