mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Moving Struts 2 dbportlet archetype
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@927409 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
INFORMATION
|
||||
===========
|
||||
- This is Struts 2's "Database Portlet" Maven Archetype
|
||||
- To be used to create a simple portlet that displays information in a database and can be deployed as a webapp or JSR168 portlet.
|
||||
|
||||
|
||||
USAGE
|
||||
=====
|
||||
|
||||
- change directory to where the portlet template is to be created,
|
||||
for example, a 'projects' directory (which you may need to create):
|
||||
|
||||
cd /home/tmjee/projects
|
||||
|
||||
- run the following command to generate the sample portlet template, assuming :-
|
||||
- root java package -> com.myCompany.myPortlet
|
||||
|
||||
- the portlet war name -> myWebApp
|
||||
|
||||
|
||||
mvn archetype:create -DgroupId=com.myCompany.myPortlet \
|
||||
-DartifactId=myWebApp \
|
||||
-DarchetypeGroupId=org.apache.struts \
|
||||
-DarchetypeArtifactId=struts2-archetype-dbportlet \
|
||||
-DarchetypeVersion=2.0.9-SNAPSHOT \
|
||||
-DremoteRepositories=http://people.apache.org/maven-snapshot-repository
|
||||
|
||||
- to compile, execute
|
||||
mvn compile
|
||||
|
||||
- to run test cases execute
|
||||
mvn test
|
||||
|
||||
- to clean execute
|
||||
mvn clean
|
||||
|
||||
- to package execute
|
||||
mvn package
|
||||
|
||||
- to start it with jetty execute as a servlet
|
||||
mvn jetty:run
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<parent>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-parent</artifactId>
|
||||
<version>2.2.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>struts2-archetype-dbportlet</artifactId>
|
||||
<version>2.2.0-SNAPSHOT</version>
|
||||
<name>Struts 2 Archetypes - Database Portlet</name>
|
||||
|
||||
<scm>
|
||||
<connection>scm:svn:http://svn.apache.org/repos/asf/struts/maven/trunk/struts2-archetype-dbportlet/</connection>
|
||||
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/struts/maven/trunk/struts2-archetype-dbportlet/</developerConnection>
|
||||
<url>http://svn.apache.org/viewcvs.cgi/struts/maven/trunk/struts2-archetype-dbportlet/</url>
|
||||
</scm>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>release</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<archetype>
|
||||
<id>struts2-archetype-dbblank</id>
|
||||
<sources>
|
||||
<source>src/main/java/view/IndexAction.java</source>
|
||||
<source>src/main/java/edit/IndexAction.java</source>
|
||||
</sources>
|
||||
<resources>
|
||||
<resource>src/main/resources/struts.xml</resource>
|
||||
<resource>src/main/webapp/index.jsp</resource>
|
||||
<resource>src/main/webapp/WEB-INF/web.xml</resource>
|
||||
<resource>src/main/webapp/WEB-INF/portlet.xml</resource>
|
||||
<resource>src/main/webapp/WEB-INF/applicationContext.xml</resource>
|
||||
<resource>src/main/webapp/WEB-INF/view/index.jsp</resource>
|
||||
<resource>src/main/webapp/WEB-INF/help/index.jsp</resource>
|
||||
<resource>src/main/webapp/WEB-INF/edit/index-input.jsp</resource>
|
||||
<resource>src/main/webapp/WEB-INF/edit/index-success.jsp</resource>
|
||||
</resources>
|
||||
</archetype>
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>${groupId}</groupId>
|
||||
<artifactId>${artifactId}</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>${version}</version>
|
||||
<name>Struts 2 Database Portlet</name>
|
||||
<url>http://www.myComp.com</url>
|
||||
<description>Struts 2 Database Portlet</description>
|
||||
|
||||
<properties>
|
||||
<struts2.version>2.1.8.1</struts2.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Struts 2 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-core</artifactId>
|
||||
<version>${struts2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-portlet-plugin</artifactId>
|
||||
<version>${struts2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-convention-plugin</artifactId>
|
||||
<version>${struts2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-spring-plugin</artifactId>
|
||||
<version>${struts2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-config-browser-plugin</artifactId>
|
||||
<version>${struts2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>2.5.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Database driver (change for your specific database) -->
|
||||
<dependency>
|
||||
<groupId>hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>1.8.0.7</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Servlet, Jsp, and Portlet API's -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
<version>2.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jsp-api</artifactId>
|
||||
<version>2.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>portlet-api</groupId>
|
||||
<artifactId>portlet-api</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.5</source>
|
||||
<target>1.5</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.mortbay.jetty</groupId>
|
||||
<artifactId>maven-jetty-plugin</artifactId>
|
||||
<version>6.1.21</version>
|
||||
<configuration>
|
||||
<scanIntervalSeconds>10</scanIntervalSeconds>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>portlet-api</groupId>
|
||||
<artifactId>portlet-api</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package ${package}.edit;
|
||||
|
||||
import org.apache.struts2.dispatcher.DefaultActionSupport;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class IndexAction extends DefaultActionSupport {
|
||||
private String pref1;
|
||||
private String pref2;
|
||||
private boolean submit;
|
||||
|
||||
public String execute() {
|
||||
|
||||
if (submit) {
|
||||
|
||||
// Save the preferences somehow
|
||||
|
||||
return SUCCESS;
|
||||
} else {
|
||||
|
||||
// Preform any logic here
|
||||
|
||||
return INPUT;
|
||||
}
|
||||
}
|
||||
|
||||
public void setSubmit(String val) {
|
||||
this.submit = (val != null);
|
||||
}
|
||||
|
||||
public String getPref1() {
|
||||
return pref1;
|
||||
}
|
||||
public String getPref2() {
|
||||
return pref2;
|
||||
}
|
||||
|
||||
public void setPref1(String pref) {
|
||||
this.pref1 = pref;
|
||||
}
|
||||
|
||||
public void setPref2(String pref) {
|
||||
this.pref2 = pref;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package ${package}.view;
|
||||
|
||||
import org.apache.struts2.dispatcher.DefaultActionSupport;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Loads and displays the contents of a database table
|
||||
*/
|
||||
public class IndexAction extends DefaultActionSupport {
|
||||
|
||||
private static boolean initialized = false;
|
||||
private DataSource ds;
|
||||
private static List<Map<String,Object>> data;
|
||||
private static long lastLoaded;
|
||||
private static long CACHE_TIME = 1000 * 60;
|
||||
|
||||
public void setDataSource(DataSource ds) {
|
||||
|
||||
// Initializes the in-memory database (not necessary in production)
|
||||
if (!initialized) {
|
||||
SimpleJdbcTemplate jt = new SimpleJdbcTemplate(ds);
|
||||
jt.update("CREATE TABLE sample_table ( id INTEGER IDENTITY, str_col VARCHAR(256), num_col INTEGER)");
|
||||
jt.update("INSERT INTO sample_table(str_col,num_col) VALUES('Ford', 100)");
|
||||
jt.update("INSERT INTO sample_table(str_col,num_col) VALUES('Toyota', 200)");
|
||||
jt.update("INSERT INTO sample_table(str_col,num_col) VALUES('Mazda', 300)");
|
||||
initialized = true;
|
||||
}
|
||||
this.ds = ds;
|
||||
}
|
||||
|
||||
public String execute() {
|
||||
|
||||
// Only refresh the data every minute as needed
|
||||
long now = System.currentTimeMillis();
|
||||
if (lastLoaded + CACHE_TIME < now) {
|
||||
SimpleJdbcTemplate jt = new SimpleJdbcTemplate(ds);
|
||||
data = jt.queryForList("SELECT * FROM sample_table");
|
||||
lastLoaded = now;
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
public List<Map<String,Object>> getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
|
||||
<!DOCTYPE struts PUBLIC
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
|
||||
|
||||
<struts>
|
||||
<constant name="struts.convention.default.parent.package" value="struts-portlet-default" />
|
||||
<constant name="struts.convention.result.path" value="/WEB-INF/" />
|
||||
<constant name="struts.devMode" value="true" />
|
||||
<constant name="struts.convention.package.locators.basePackage" value="${package}"/>
|
||||
|
||||
<include file="struts-portlet-default.xml" />
|
||||
|
||||
<package name="${artifactId}-package" extends="struts-portlet-default" namespace="/">
|
||||
<action name="*">
|
||||
<result type="redirectAction">
|
||||
<param name="actionName">index</param>
|
||||
<param name="namespace">/view</param>
|
||||
</result>
|
||||
</action>
|
||||
</package>
|
||||
</struts>
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
|
||||
|
||||
<!--
|
||||
Defines the datasource that will be injected into your Actions
|
||||
-->
|
||||
|
||||
<beans default-autowire="byName">
|
||||
|
||||
<!-- Change for your database -->
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
destroy-method="close">
|
||||
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="url" value="jdbc:hsqldb:mem:example"/>
|
||||
<property name="username" value="sa"/>
|
||||
<property name="password" value=""/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<%@ taglib prefix="s" uri="/struts-tags" %>
|
||||
<h1>StrutsPortlet</h1>
|
||||
This is the default edit page!
|
||||
<p />
|
||||
|
||||
<s:form action="index" method="POST">
|
||||
<s:textfield label="Preference one" name="preferenceOne" value="%{preferenceOne}"/>
|
||||
<s:textfield label="Preference two" name="preferenceTwo" value="%{preferenceTwo}"/>
|
||||
<s:submit name="submit" value="Save prefs"/>
|
||||
</s:form>
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<%@ taglib prefix="s" uri="/struts-tags" %>
|
||||
|
||||
The preferences has been saved:
|
||||
|
||||
<p />
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Value</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Preference 1</td>
|
||||
<td><s:property value="%{pref1}" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Preference 2</td>
|
||||
<td><s:property value="%{pref2}" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<a href="<s:url action="index"/>">Back</a>
|
||||
+1
@@ -0,0 +1 @@
|
||||
This is the default help page!
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
<portlet-app version="1.0" xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" id="struts-portlet">
|
||||
<portlet id="StrutsDBPortlet">
|
||||
<description xml:lang="EN">Struts Database Portlet</description>
|
||||
<portlet-name>StrutsDBPortlet</portlet-name>
|
||||
<display-name xml:lang="EN">Struts Database Portlet</display-name>
|
||||
|
||||
<portlet-class>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</portlet-class>
|
||||
|
||||
<init-param>
|
||||
<name>actionPackages</name>
|
||||
<value>${package}</value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<!-- The view mode namespace. Maps to a namespace in the xwork config file -->
|
||||
<name>viewNamespace</name>
|
||||
<value>/view</value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<!-- The default action to invoke in view mode -->
|
||||
<name>defaultViewAction</name>
|
||||
<value>index</value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<!-- The view mode namespace. Maps to a namespace in the xwork config file -->
|
||||
<name>editNamespace</name>
|
||||
<value>/edit</value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<!-- The default action to invoke in view mode -->
|
||||
<name>defaultEditAction</name>
|
||||
<value>index</value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<!-- The view mode namespace. Maps to a namespace in the xwork config file -->
|
||||
<name>helpNamespace</name>
|
||||
<value>/help</value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<!-- The default action to invoke in view mode -->
|
||||
<name>defaultHelpAction</name>
|
||||
<value>index</value>
|
||||
</init-param>
|
||||
|
||||
<expiration-cache>0</expiration-cache>
|
||||
|
||||
<supports>
|
||||
<mime-type>text/html</mime-type>
|
||||
<portlet-mode>edit</portlet-mode>
|
||||
<portlet-mode>help</portlet-mode>
|
||||
</supports>
|
||||
|
||||
<supported-locale>en</supported-locale>
|
||||
|
||||
<portlet-info>
|
||||
<title>Struts Database Porlet</title>
|
||||
<short-title>DB</short-title>
|
||||
<keywords>struts,portlet,database</keywords>
|
||||
</portlet-info>
|
||||
</portlet>
|
||||
|
||||
</portlet-app>
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<%@ taglib prefix="s" uri="/struts-tags" %>
|
||||
<H2>Welcome to the Struts data table portlet</H2>
|
||||
<p/>
|
||||
This a simple table showing the database data:
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Value</th>
|
||||
</tr>
|
||||
<s:iterator id="row" value="%{data}">
|
||||
<tr>
|
||||
<td><s:property value="%{STR_COL}" /></td>
|
||||
<td><s:property value="%{NUM_COL}" /></td>
|
||||
</tr>
|
||||
</s:iterator>
|
||||
</table>
|
||||
<ul>
|
||||
<li><a href="<s:url action="index" portletMode="edit" namespace="/edit"/>">Go to edit mode and see what's there</a></li>
|
||||
<li><a href="<s:url action="index" portletMode="help" namespace="/help"/>">Go to help mode and see what's there</a></li>
|
||||
</ul>
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
|
||||
<web-app>
|
||||
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/applicationContext*.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
<filter id="filterdispatcher">
|
||||
<filter-name>struts</filter-name>
|
||||
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
|
||||
</filter>
|
||||
|
||||
<filter-mapping>
|
||||
<filter-name>struts</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.apache.struts2.portlet.context.ServletContextHolderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<servlet id="preparator">
|
||||
<servlet-name>preparator</servlet-name>
|
||||
<servlet-class>org.apache.struts2.portlet.context.PreparatorServlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.jsp</welcome-file>
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
<%
|
||||
String redirectURL = "view/index.action";
|
||||
response.sendRedirect(redirectURL);
|
||||
%>
|
||||
Reference in New Issue
Block a user