马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622 。
您需要 登录 才可以下载或查看,没有帐号?注册
x
Liferay研究之二:引入Spring 收藏
首先Suppose你已经搭建好了ext环境。 1、在Ext/ext-web/docroot/WEB-INF/web.xml中引入spring
<display-name>Spring Portal</display-name>
<description>Spring Portlet sample application</description>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderServlet.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>view-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>view-servlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
2、在/WEB-INF/下建立一个目录(当然也可以不建)context,并在其下建立一个文件applicationContext.xml,配置spring的缺省行为,如下所示: <!-- 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> <!-- This interceptor forwards the mapping request parameter from an ActionRequest to be used as a render parameter. --> <bean id="parameterMappingInterceptor" class="org.springframework.web.portlet.handler.ParameterMappingInterceptor"/> <!-- Abstract Default ExceptionHandler --> <bean id="defaultExceptionHandlerTemplate" class="org.springframework.web.portlet.handler.SimpleMappingExceptionResolver" abstract="true">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<props>
<prop key="javax.portlet.PortletSecurityException">unauthorized</prop>
<prop key="javax.portlet.UnavailableException">unavailable</prop>
</props>
</property>
</bean> 3、编写一个Spring Portlet。 在ext-impl/新建一个Class,目录自选,命名为HelloWorldPortlet, 该类继承于GenericPortlet。 public class HelloWorldPortlet extends GenericPortlet ...{
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException ...{
System.out.println("Entering HelloWorldPortlet.doView");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello World</h1>");
out.println("<p>This portlet demonstrates how to delegate to "+
"an existing JSR-168 portlet via a HandlerAdapter</p>");
out.println("<p>Portlet Name: " + this.getPortletName() + "</p>");
out.println("<p>Init Parameters:</p><ul>");
for (Enumeration e = this.getInitParameterNames(); e.hasMoreElements();) ...{
String name = (String)e.nextElement();
out.println("<li>" + name + " = " + this.getInitParameter(name) + "</li>");
}
out.println("</ul>");
}
} 4、编写HelloWorldPortlet对应的Spring bean配置。在context目录下建立一个helloworld.xml,并配置其内容如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans>
<bean id="helloWorldPortlet" class="org.springframework.web.portlet.mvc.PortletWrappingController">
<property name="portletClass">
<value>com.ext.portlet.helloworld.HelloWorldPortlet</value>
</property>
<property name="useSharedPortletConfig">
<value>false</value>
</property>
<property name="portletName">
<value>wrapped-hello-world</value>
</property>
<property name="initParameters">
<props>
<prop key="HarryPotter">The magic property</prop>
<prop key="JerrySeinfeld">The funny property</prop>
</props>
</property>
</bean>
<!-- Handler Mapping -->
<bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
<property name="portletModeMap">
<map>
<entry key="view"><ref bean="helloWorldPortlet"/></entry>
</map>
</property>
</bean>
<!-- Exceptions Handler -->
<bean id="defaultExceptionHandler" parent="defaultExceptionHandlerTemplate"/>
</beans> 注意,此处需要配置一个缺省的handler. 5、至此,一个Spring Portlet就开发好了,剩下的,就是需要将其配置成为一个Liferay的Portlet,具体步骤如下: 6、先在portlet-ext.xml中定义Portlet: <portlet>
<portlet-name>helloworld</portlet-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/context/helloworld.xml</value>
</init-param>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>Hello World</title>
</portlet-info>
</portlet> 注意,上面加粗部分显示了liferay如何将spring bean定义配置文件导入的。有一些文章介绍需要在portal-ext.properties中配置,可能是版本升级了,在我测试过程中,没有用那种方式。 7、然后在将其定义给Liferay,在liferay-portlet-ext.xml中定义: <portlet>
<portlet-name>helloworld</portlet-name>
</portlet>
8、将其显示到Add Content列表中。 在liferay-display.xml中定义: <category name="category.sample">
<portlet id="helloworld" />
</category> 至此,Spring Portlet开发完成。最后,还需要将spring-portlet.jar拷贝到classpath中。OK, ant deploy→login→add content→sample.helloworld。 打完,收工! 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/smilingleo/archive/2007/10/31/1859922.aspx |