Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Last active December 16, 2019 04:53
Show Gist options
  • Select an option

  • Save A-pZ/aaa7c6b96f40a49a438f71f5b6c85fba to your computer and use it in GitHub Desktop.

Select an option

Save A-pZ/aaa7c6b96f40a49a438f71f5b6c85fba to your computer and use it in GitHub Desktop.
Struts2.3~で表形式の入出力を行う(JSP編)
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="update" theme="simple">
<s:iterator value="users" var="user" status="status">
<s:textfield name="users[%{#status.index}].name"></s:textfield>
<s:textfield name="users[%{#status.index}].weight"></s:textfield>
</s:iterator>
<s:submit />
</s:form>
</body>
</html>
/**
*
*/
package com.github.apz.struts2.sample.arrays.actions;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author a-pz
*
*/
@Namespace("/")
@ParentPackage("struts-default")
@Results({
@Result(name = ActionSupport.SUCCESS, location = "array.jsp" , type="dispatcher"),
})
@ExceptionMappings({
@ExceptionMapping(exception="java.lang.Exception" , result="exception")
})
public class ArrayDisplayAction extends ActionSupport {
@Action("display")
public String display() throws Exception {
List<UserModel> users = new ArrayList<UserModel>() {{
add(UserModel.of("ロン", 61));
add(UserModel.of("リンゴ", 52));
add(UserModel.of("ポテト", 65));
}};
this.users = users;
return ActionSupport.SUCCESS;
}
@Action("update")
public String update() throws Exception {
return ActionSupport.SUCCESS;
}
private List<UserModel> users;
public List<UserModel> getUsers() {
return users;
}
public void setUsers(List<UserModel> users) {
this.users = users;
}
}
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.apz.struts2.sample</groupId>
<artifactId>arrays</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>arrays</name>
<properties>
<struts2.version>2.5.22</struts2.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</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-junit-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<stopKey>CTRL+C</stopKey>
<stopPort>8999</stopPort>
<systemProperties>
<systemProperty>
<name>xwork.loggerFactory</name>
<value>com.opensymphony.xwork2.util.logging.log4j2.Log4j2LoggerFactory</value>
</systemProperty>
</systemProperties>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppSourceDirectory>${basedir}/src/main/webapp/</webAppSourceDirectory>
<webAppConfig>
<descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.github.apz.struts2.sample.arrays.actions;
public class UserModel {
public UserModel() {
// NOP
}
public UserModel(String name, Integer weight) {
this.name = name;
this.weight = weight;
}
public static UserModel of(String name, Integer weight) {
return new UserModel(name, weight);
}
private String name;
private Integer weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank Convention</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment