问题
I am absolutly new in LifeRay portlet development and I have some problem to create an "Hello World" test portlet into a project on which I am working on.
In this project it is used Structs 2 to do the dispatchment.
So I have done the following operations:
1) I create the TestPortlet1Action class into this folder: /Web/src/main/java/mypackage/actions/ where I define the startTestPortlet1() method that is the starting method of my protlet (I think that this is the portlet entry point):
package egp.prc.km.actions.testPortlet1;
import egp.prc.km.actions.KMAction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
@Controller("TestPortlet1")
@Scope("prototype")
public class TestPortlet1Action extends KMAction {
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
private String message;
public String startTestPortlet1() {
this.setMessage("Hello World !!!");
return SUCCESS;
}
}
where KMAction provde me this Action interface:
package com.opensymphony.xwork2;
public interface Action {
java.lang.String SUCCESS = "success";
java.lang.String NONE = "none";
java.lang.String ERROR = "error";
java.lang.String INPUT = "input";
java.lang.String LOGIN = "login";
java.lang.String execute() throws java.lang.Exception;
}
So the return SUCCESS statment return the success string.
2) Then I have create a JSP view for this portlet named testPortlet1.jsp (into thi folder : /Web/src/main/webapp/testPortlet1/testPortlet1.jsp):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags" %>
<style>
.portlet-topper#portlet_SliderNewsPortlet_WAR_KMPortlets, .portlet-title#portlet_SliderNewsPortlet_WAR_KMPortlets {
display: none
}
.portlet#portlet_SliderNewsPortlet_WAR_KMPortlets, .portlet-content#portlet_SliderNewsPortlet_WAR_KMPortlets {
border: 0;
margin: 0;
padding: 0;
background: 0;
}
</style>
<hr style="height:1px;border:none;color:#1d8843;background-color:#1d8843;width: 98%" size="4px">
<table width="100%">
<tr>
<td>
<table class="homePortletTitle">
<tr>
<td style="padding-bottom: 0px;"><img src="/km-theme/images/km-icons/section_icon_focus.png"></td>
<th style="">TestPortlet1</th>
<td style="">Hello World Portlet</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p>Hello World !!!</p>
</td>
</tr>
</table>
3) So now I know that I have to update the struts.xml file (that it is into Web/main/resources folder) that contains something like:
<struts>
<constant name="struts.objectFactory" value="spring"/>
<constant name="struts.devMode" value="false"/>
<constant name="struts.locale" value="en_US"/>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
<constant name="struts.custom.i18n.resources" value="km_international"/>
<package name="kmAjaxPackage" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="liferayAjax" class="liferayAjaxInterceptor"/>
<interceptor-stack name="kmAjaxStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="liferayAjax"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="kmAjaxStack"/>
</package>
<include file="chat-struts.xml"/>
..................................
..................................
..................................
<include file="TestPortlet1-struts.xml"/>
</struts>
As you can see I have included the struts configuration file related to my hello world portlet, this one:
<include file="TestPortlet1-struts.xml"/>
4) So now I have configured the TestPortlet1-struts.xml file, in this way:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="TestPortlet1Package" extends="struts-portlet-default" namespace="/testPortlet1">
<action name="startFocusAction" class="FocusAction" method="startFocus">
<result name="success">/testPortlet1/testPortlet1.jsp</result>
</action>
</package>
</struts>
5) Now my problem is to understand how correctly configure the web.xml file (that I have into the folder **\Web\src\main\webapp\WEB-INF**). From what I understand in this file I have the mapping with the Struts 2 servlets (or am I wrong?)
So I have something like this (for another working servlet):
<servlet>
<servlet-name>calendar</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortletServlet</servlet-class>
<init-param>
<param-name>portlet-class</param-name>
<param-value>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>calendar</servlet-name>
<url-pattern>/calendar/*</url-pattern>
</servlet-mapping>
So the first section definied the servlet and the second section definied the URL pattern mapped to be handled by this servlet. Is it true or am I missing something?
So what have I to do to configure my previous "Hello World test" servlet? Is this ok?
<servlet>
<servlet-name>testPortlet1</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortletServlet</servlet-class>
<init-param>
<param-name>portlet-class</param-name>
<param-value>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testPortlet1</servlet-name>
<url-pattern>/testPortlet1/*</url-pattern>
</servlet-mapping>
Is this ok or am I missing something?
6) Then I have to configure the portlet.xml file into the folder **\Web\src\main\webapp\WEB-INF**, adding:
<portlet>
<portlet-name>testPortlet1</portlet-name>
<display-name>testPortlet1</display-name>
<portlet-class>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</portlet-class>
<init-param>
<name>viewNamespace</name>
<value>/testPortlet1</value>
</init-param>
<init-param>
<name>defaultViewAction</name>
<!--<value>savePhoto</value>-->
<value>startTestPortlet1</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<portlet-info>
<title>testPortlet1</title>
</portlet-info>
</portlet>
where startTestPortlet1 represent the method startTestPortlet1() definied in my action class.
7) Then i add this statment into the liferay-portlet.xml configuration file:
<portlet>
<portlet-name>testpPortlet1</portlet-name>
<instanceable>false</instanceable>
</portlet>
8) Finally I configured the file liferay-display.cml to set the categories of my portlet, something like this:
<?xml version="1.0"?>
<!DOCTYPE display PUBLIC
"-//Liferay//DTD Display 6.0.0//EN"
"http://www.liferay.com/dtd/liferay-display_6_0_0.dtd">
<display>
<category name="First category">
<category name="Second inner category">
<category name="Third inner category">
<category name="chat">
<portlet id="ChatNavigatorUser"/>
<portlet id="ChatNavigator"/>
</category>
<category name="testcategory">
<portlet id="TestPortlet1"/>
<portlet id="testPortlet1"/>
</category>
</category>
</category>
</category>
</display>
The problem is that when I try to add my portlet on the homepage of the portal I obtain this contet instead the expected Hello World output:
testPortlet1 is temporarily unavailable.
回答1:
you have to configure the web.xml like below:
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>com.liferay.portal.kernel.spring.context.PortletContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>struts2servlet</servlet-name>
<servlet-class>org.apache.struts2.dispatcher.ng.servlet.StrutsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>struts2servlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>struts2Resources</servlet-name>
<servlet-class>org.apache.struts2.dispatcher.ng.servlet.StrutsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>struts2Resources</servlet-name>
<url-pattern>/struts/*</url-pattern>
</servlet-mapping>
</web-app>
got it from http://www.roytuts.com/integrate-struts-2-in-liferay-portlet/
来源:https://stackoverflow.com/questions/27562829/why-my-first-hello-world-liferay-portlet-result-to-be-temporarily-unavailable