lifiray5.1.2 struts2 portlet

建立web工程,工程结构如图:





TestAction.java
package com.shrek;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private String username;

	public String login(){
		HttpServletRequest reqeust =  ServletActionContext.getRequest();
		System.out.println("=================login============="+username);
		return "ok";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}


struts.xml
<?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>

	<constant name="struts.enable.DynamicMethodInvocation"
		value="false" />
	<constant name="struts.devMode" value="false" />
//此处需继承extends="struts-portlet-default"
	<package name="view" namespace="/view"
		extends="struts-portlet-default">
		<action name="index"
			class="com.opensymphony.xwork2.ActionSupport">
			<result name="success">/view.jsp</result>
		</action>
		<action name="testlogin" method="login"
			class="com.shrek.TestAction">
			<result name="ok">/my.jsp</result>
		</action>
	</package>


	<package name="edit" namespace="/edit"
		extends="struts-portlet-default">
		<action name="index"
			class="com.opensymphony.xwork2.ActionSupport">
			<result name="success">/edit.jsp</result>
		</action>
		<action name="test_*" method="{1}"
			class="com.shrek.TestAction">
			<result name="ok">/my.jsp</result>
		</action>
	</package>

	<package name="help" namespace="/help"
		extends="struts-portlet-default">
		<action name="index"
			class="com.opensymphony.xwork2.ActionSupport">
			<result name="success">/help.jsp</result>
		</action>
		<action name="test_*" method="{1}"
			class="com.shrek.TestAction">
			<result name="ok">/my.jsp</result>
		</action>
	</package>


	<!-- Add packages here -->

</struts>


liferay-display.xml
<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.1.0//EN" "http://www.liferay.com/dtd/liferay-display_5_1_0.dtd">

<display>
	<category name="struts2">
		<portlet id="xxx" />
	</category>
</display>


liferay-plugin-package.properties
name=StrutsPortlet
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=MIT



liferay-plugin-package.xml
<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.1.0//EN" "http://www.liferay.com/dtd/liferay-display_5_1_0.dtd">

<plugin-package>
	<name>StrutsPortlet</name>
	<module-id>liferay/struts/5.1.1.9/war</module-id>
	<types>
		<type>portlet</type>
	</types>
	<tags>
		<tag>struts 2</tag>
	</tags>
	<short-description>Struts 2 Portlets</short-description>
	<long-description></long-description>
	<change-log>Adapted to the latest version of Liferay</change-log>
	<page-url>http://www.liferay.com/web/guest/downloads</page-url>
	<author>Cignex, Inc.</author>
	<licenses>
		<license osi-approved="true">MIT</license>
	</licenses>
	<liferay-versions>
		<liferay-version>5.1.1+</liferay-version>
	</liferay-versions>
</plugin-package>


liferay-portlet.xml

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 5.1.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_5_1_0.dtd">

<liferay-portlet-app>
	<portlet>
		<portlet-name>xxx</portlet-name>
		<portlet-url-class>
			com.liferay.portal.apache.bridges.struts.LiferayStrutsPortletURLImpl
		</portlet-url-class>
		<layout-cacheable>true</layout-cacheable>
		<instanceable>false</instanceable>
		<add-default-resource>true</add-default-resource>
	</portlet>
	<role-mapper>
		<role-name>administrator</role-name>
		<role-link>Administrator</role-link>
	</role-mapper>
	<role-mapper>
		<role-name>guest</role-name>
		<role-link>Guest</role-link>
	</role-mapper>
	<role-mapper>
		<role-name>power-user</role-name>
		<role-link>Power User</role-link>
	</role-mapper>
	<role-mapper>
		<role-name>user</role-name>
		<role-link>User</role-link>
	</role-mapper>
</liferay-portlet-app>


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>xxx</portlet-name>

		<display-name>StrutsPortlet</display-name>

		<portlet-class>
			org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher
		</portlet-class>

		<init-param>
//这里的名字是固定的,不可以改动,portlet会自动寻找viewNamespace、defaultViewAction
			<name>viewNamespace</name>

			<value>/view</value>

		</init-param>

		<init-param>

			<name>editNamespace</name>

			<value>/edit</value>

		</init-param>

		<init-param>

			<name>defaultViewAction</name>

			<value>index</value>

		</init-param>

		<init-param>

			<name>defaultEditAction</name>

			<value>index</value>

		</init-param>

		<supports>

			<mime-type>text/html</mime-type>

			<portlet-mode>view</portlet-mode>

			<portlet-mode>edit</portlet-mode>

		</supports>

		<portlet-info>

			<title>StrutsPortlet</title>

			<short-title>StrutsPortlet</short-title>

			<keywords>StrutsPortlet</keywords>

		</portlet-info>

	</portlet>

</portlet-app>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">

	<filter>
		<description>struts2</description>
		<display-name>struts2</display-name>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>



jsp action 提交及显示
form必须使用s标签,因为需要写明命名空间(namespace);portlet取reqeust中的值,无法用el表达式,在struts2中,可以使用s(s:property)标签取值。
<s:form action="testlogin" method="post" namespace="view">
		    view<input type="text" name="username" />
			<br>
			<input type="submit" value="submit" />
			 <s:property value="username"/>
		</s:form>

你可能感兴趣的:(java,apache,xml,jsp,struts)