|
解凍するファイル
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all.zip 解凍するとできるフォルダ C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0 |
|
解凍するファイル
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0.war 解凍するとできるフォルダ C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0 |
|
コピー元
C:\Users\(ユーザ名)\Downloads\struts-7.0.0-all\struts-7.0.0\apps\struts2-rest-showcase-7.0.0\WEB-INF\lib\* コピー先 C:\pleiades\2024-12\workspace\(プロジェクト名)\src\main\webapp\WEB-INF\lib (重要)コピー先から以下のjarファイルを削除 struts2-rest-plugin-7.0.0.jar |
|
package test8.sample.model; import java.util.Arrays; /** * Models a Person who registers. */ public class Person { private String firstName; private String lastName; private String sport; private String gender; private String residency; private boolean over21; private String[] carModels; private String preferredCarModel; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setSport(String sport) { this.sport = sport; } public void setGender(String gender) { this.gender = gender; } public String getGender() { return gender; } public String getSport() { return sport; } public void setResidency(String residency) { this.residency = residency; } public String getResidency() { return residency; } public void setOver21(boolean over21) { this.over21 = over21; } public boolean isOver21() { return over21; } public void setCarModels(String[] carModels) { this.carModels = carModels; } public String[] getCarModels() { return carModels; } public String getPreferredCarModel() { return preferredCarModel; } public void setPreferredCarModel(String preferredCarModel) { this.preferredCarModel = preferredCarModel; } public String toString() { return "First Name: " + getFirstName() + " | " + " Last Name: " + getLastName() + " | " + " Favorite Sport: " + getSport() + " | " + " Gender: " + getGender() + " | " + " Residency: " + getResidency() + " | " + " Over 21: " + isOver21() + " | " + " Car models: " + Arrays.asList(getCarModels()) + " | " + " Preferred Car Model: " + getPreferredCarModel() ; } } |
|
package test8.sample.model; public class State { private String stateAbbr; private String stateName; public State(String stateAbbr, String stateName) { this.stateAbbr = stateAbbr; this.stateName = stateName; } public void setStateAbbr(String stateAbbr) { this.stateAbbr = stateAbbr; } public String getStateAbbr() { return stateAbbr; } public void setStateName(String stateName) { this.stateName = stateName; } public String getStateName() { return stateName; } public String toString() { return getStateAbbr(); } } |
|
package test8.sample.service; import test8.sample.model.Person; public interface EditService { Person getPerson(); void savePerson(Person personBean); } |
|
package test8.sample.service; import test8.sample.model.Person; /** * Implement Services needed to edit and save * a Person object's state. In this implementation * the Person object's state is stored in memory */ public class EditServiceInMemory implements EditService { private static final Person person; private static final String[] carModels = {"Ford", "Nissan"}; static { person = new Person(); person.setFirstName("Bruce"); person.setLastName("Phillips"); person.setSport("basketball"); person.setGender("not sure"); person.setResidency("KS"); person.setOver21(true); person.setCarModels(carModels); person.setPreferredCarModel(""); } public Person getPerson() { return EditServiceInMemory.person; } public void savePerson(Person personBean) { EditServiceInMemory.person.setFirstName(personBean.getFirstName()); EditServiceInMemory.person.setLastName(personBean.getLastName()); EditServiceInMemory.person.setSport(personBean.getSport()); EditServiceInMemory.person.setGender(personBean.getGender()); EditServiceInMemory.person.setResidency(personBean.getResidency()); EditServiceInMemory.person.setOver21(personBean.isOver21()); EditServiceInMemory.person.setCarModels(personBean.getCarModels()); EditServiceInMemory.person.setPreferredCarModel(personBean.getPreferredCarModel()); } } |
|
package test8.sample.action; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.struts2.ActionSupport; import org.apache.struts2.interceptor.parameter.StrutsParameter; import test8.sample.model.Person; import test8.sample.model.State; import test8.sample.service.EditService; import test8.sample.service.EditServiceInMemory; /** * Acts as a controller to handle actions * related to editing a Person. */ public class EditAction extends ActionSupport { private final EditService editService = new EditServiceInMemory(); private Person personBean; private final String[] sports = {"football", "baseball", "basketball"}; private final String[] genders = {"male", "female", "not sure"}; private final String[] carModelsAvailable = {"Ford", "Chrysler", "Toyota", "Nissan"}; public String execute() throws Exception { editService.savePerson(getPersonBean()); return SUCCESS; } public String input() throws Exception { setPersonBean(editService.getPerson()); return INPUT; } @StrutsParameter(depth = 1) public Person getPersonBean() { return personBean; } public void setPersonBean(Person person) { personBean = person; } public List return Arrays.asList(sports); } public List return Arrays.asList(genders); } public List List states.add(new State("AZ", "Arizona")); states.add(new State("CA", "California")); states.add(new State("FL", "Florida")); states.add(new State("KS", "Kansas")); states.add(new State("NY", "New York")); return states; } public String[] getCarModelsAvailable() { return carModelsAvailable; } } |
|
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html charset="UTF-8"> <head> <s:head/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts 2 Form Tags - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <p><a href='<s:url action="editInput" namespace='' />'>Edit your information</a></p> </body> </html> |
|
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html charset="UTF-8"> <head> <s:head/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts 2 Form Tags - Edit Person</title> <style> .tdLabel {text-align:left; vertical-align:top; } </style> </head> <body> <h1>Update Information</h1> <p>Use the form below to edit your information.</p> <s:form action="save" method="post"> <s:textfield labelSeparator=" " key="personBean.firstName"/> <s:textfield labelSeparator=" " key="personBean.lastName"/> <s:select labelSeparator=" " key="personBean.sport" list="sports"/> <s:radio labelSeparator=" " key="personBean.gender" list="genders"/> <s:select labelSeparator=" " key="personBean.residency" list="states" listKey="stateAbbr" listValue="stateName"/> <s:checkbox labelSeparator=" " key="personBean.over21"/> <s:checkboxlist labelSeparator=" " key="personBean.carModels" list="carModelsAvailable"/> <s:radio labelSeparator=" " name="personBean.preferredCarModel" label="Status" list="carModelsAvailable"/> <s:submit key="submit"/> </s:form> </body> </html> |
|
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html charset="UTF-8"> <head> <s:head/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Update Successful</title> </head> <body> <h1>Updated Information</h1> <p>Your information: <s:property value="personBean"/></p> <p><a href="<s:url action='index' />">Return to home page</a>.</p> </body> </html> |
|
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 6.0//EN" "http://struts.apache.org/dtds/struts-6.0.dtd"> <struts> <constant name="struts.devMode" value="true"/> <constant name="struts.allowlist.packageNames" value="test8.sample" /> <package name="test8" extends="struts-default"> <default-action-ref name="index"/> <!-- If no class attribute is specified the framework will assume success and render the result index.jsp --> <!-- If no name value for the result node is specified the success value is the default --> <action name="index"> <result>/index.jsp</result> </action> <action name="editInput" class="test8.sample.action.EditAction" method="input"> <result name="input">/edit.jsp</result> </action> <action name="save" class="test8.sample.action.EditAction" method="execute"> <result name="success">/thankyou.jsp</result> <result name="input">/edit.jsp</result> </action> </package> </struts> |
|
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> </Console> </Appenders> <Loggers> <Logger name="org.apache.struts2" level="info"/> <Logger name="test8.sample" level="debug"/> <Root level="warn"> <AppenderRef ref="STDOUT"/> </Root> </Loggers> </Configuration> |
|
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Form Tags</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> |
|
personBean.firstName=First name personBean.lastName=Last name personBean.sport=Favorite sport personBean.gender=Gender personBean.residency=State resident personBean.over21=21 or older personBean.carModels=Car models owned personBean.preferredCarModels=Preferred car model submit=Save Changes |