Skip to content

Commit a87f2f8

Browse files
authored
Merge pull request #522 from apache/struts-parameter
Defines a new struts-parameter example app to show how @StrutsParameter works
2 parents 9293ef3 + bf344d6 commit a87f2f8

10 files changed

Lines changed: 242 additions & 0 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
<module>rest-angular</module>
111111
<module>shiro-basic</module>
112112
<module>spring-struts</module>
113+
<module>struts-parameter</module>
113114
<module>text-provider</module>
114115
<module>tiles</module>
115116
<module>themes</module>

struts-parameter/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>struts-examples</artifactId>
7+
<groupId>org.apache.struts</groupId>
8+
<version>2.0.0</version>
9+
</parent>
10+
11+
<artifactId>struts-parameter</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<packaging>war</packaging>
14+
<name>struts-parameter</name>
15+
<description>A simple application demonstrating how to use @StrutsParameter annotation</description>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<maven.compiler.source>17</maven.compiler.source>
20+
<maven.compiler.target>17</maven.compiler.target>
21+
</properties>
22+
23+
<dependencies/>
24+
25+
<build>
26+
<finalName>annotations</finalName>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.eclipse.jetty</groupId>
30+
<artifactId>jetty-maven-plugin</artifactId>
31+
<version>${jetty-plugin.version}</version>
32+
<configuration>
33+
<webApp>
34+
<contextPath>/${project.artifactId}</contextPath>
35+
</webApp>
36+
<stopKey>CTRL+C</stopKey>
37+
<stopPort>8999</stopPort>
38+
<scan>10</scan>
39+
</configuration>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.apache.struts.examples.parameter;
2+
3+
public class Admin {
4+
private String username;
5+
6+
public Admin() {
7+
}
8+
9+
public Admin(String username) {
10+
this.username = username;
11+
}
12+
13+
public String getUsername() {
14+
return username;
15+
}
16+
17+
public void setUsername(String username) {
18+
this.username = username;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "Admin{" +
24+
"username='" + username + '\'' +
25+
'}';
26+
}
27+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.struts.examples.parameter;
20+
21+
import org.apache.struts2.ActionSupport;
22+
import org.apache.struts2.interceptor.parameter.StrutsParameter;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
public class IndexAction extends ActionSupport {
28+
29+
private final List<User> users = new ArrayList<>();
30+
private Admin admin;
31+
32+
public String execute() throws Exception {
33+
if (users.isEmpty()) {
34+
users.add(new User(1, "Luk"));
35+
users.add(new User(2, "Jan"));
36+
}
37+
if (admin == null) {
38+
admin = new Admin("Michal");
39+
}
40+
return SUCCESS;
41+
}
42+
43+
@StrutsParameter(depth = 2)
44+
public List<User> getUsers() {
45+
return users;
46+
}
47+
48+
@StrutsParameter(depth = 2)
49+
public Admin getAdminUser() {
50+
return admin;
51+
}
52+
53+
@StrutsParameter(depth = 2)
54+
public void setAdminUser(Admin admin) {
55+
this.admin = admin;
56+
}
57+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.apache.struts.examples.parameter;
2+
3+
public class User {
4+
5+
private int id;
6+
private String name;
7+
8+
public User() {
9+
}
10+
11+
public User(int id, String name) {
12+
this.id = id;
13+
this.name = name;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "User{" +
35+
"id=" + id +
36+
", name='" + name + '\'' +
37+
'}';
38+
}
39+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="STDOUT" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Logger name="org.apache.struts2.conversion" level="debug"/>
10+
<Logger name="org.apache.struts.examples.parameter" level="debug"/>
11+
<Root level="warn">
12+
<AppenderRef ref="STDOUT"/>
13+
</Root>
14+
</Loggers>
15+
</Configuration>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE struts PUBLIC
3+
"-//Apache Software Foundation//DTD Struts Configuration 6.0//EN"
4+
"http://struts.apache.org/dtds/struts-6.0.dtd">
5+
<struts>
6+
<constant name="struts.devMode" value="true"/>
7+
<constant name="struts.allowlist.packageNames" value="org.apache.struts.examples.parameter"/>
8+
9+
<package name="default" namespace="/" extends="struts-default">
10+
11+
<default-action-ref name="index"/>
12+
13+
<action name="index" class="org.apache.struts.examples.parameter.IndexAction">
14+
<result name="success">/WEB-INF/examples/index.jsp</result>
15+
</action>
16+
17+
</package>
18+
19+
<!-- Add addition packages and configuration here. -->
20+
</struts>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<%@ page contentType="text/html; charset=UTF-8" %>
2+
<%@ taglib prefix="s" uri="/struts-tags" %>
3+
<html>
4+
<head>
5+
<title>Home</title>
6+
</head>
7+
8+
<body>
9+
<s:form action="index" method="POST">
10+
<s:iterator value="users" status="status">
11+
<s:hidden name="users[%{#status.index}].id"/>
12+
<s:textfield name="users[%{#status.index}].name" label="User %{users[#status.index].id}" id="user_%{users[#status.index].id}"/>
13+
</s:iterator>
14+
<s:textfield name="adminUser.username" label="Admin"/>
15+
<s:submit/>
16+
</s:form>
17+
</body>
18+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
5+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
6+
version="3.1">
7+
<filter>
8+
<filter-name>struts2</filter-name>
9+
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
10+
</filter>
11+
12+
<filter-mapping>
13+
<filter-name>struts2</filter-name>
14+
<url-pattern>/*</url-pattern>
15+
</filter-mapping>
16+
17+
<welcome-file-list>
18+
<welcome-file>index.jsp</welcome-file>
19+
</welcome-file-list>
20+
21+
</web-app>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<% response.sendRedirect("index"); %>

0 commit comments

Comments
 (0)