Skip to content

Commit c3a4977

Browse files
committed
Refactor(form-processing): Update for Struts 7 compatibility
- Removed cancel2() method to prevent unnecessary value binding to personBean.age. - Corrected namespace in form action tags for accuracy. - Added @StrutsParameter annotations to action parameter methods. - Implemented OGNL allowlist to comply with stricter security policies. - Updated web.xml to schema version 6.0.
1 parent 53099a4 commit c3a4977

6 files changed

Lines changed: 13 additions & 13 deletions

File tree

form-processing/src/main/java/org/apache/struts/helloworld/action/HelloWorldAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.struts.helloworld.model.MessageStore;
44
import org.apache.struts2.ActionSupport;
5+
import org.apache.struts2.interceptor.parameter.StrutsParameter;
56

67
/**
78
* Acts as a Struts 2 controller that responds
@@ -37,6 +38,7 @@ public String getUserName() {
3738
return userName;
3839
}
3940

41+
@StrutsParameter
4042
public void setUserName(String userName) {
4143
this.userName = userName;
4244
}

form-processing/src/main/java/org/apache/struts/register/action/Register.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import org.apache.struts.register.model.Person;
44
import org.apache.struts2.ActionSupport;
5+
import org.apache.struts2.interceptor.parameter.StrutsParameter;
56

6-
import java.util.ArrayList;
77
import java.util.List;
88

99
public class Register extends ActionSupport {
@@ -17,15 +17,10 @@ public String execute2() throws Exception {
1717
}
1818

1919
public String input() throws Exception {
20-
this.options = new ArrayList<>();
21-
this.options.add(3);
2220
return INPUT;
2321
}
2422

25-
public String cancel2() throws Exception {
26-
return SUCCESS;
27-
}
28-
23+
@StrutsParameter(depth = 1)
2924
public Person getPersonBean() {
3025
return personBean;
3126
}
@@ -34,6 +29,7 @@ public void setPersonBean(Person person) {
3429
personBean = person;
3530
}
3631

32+
@StrutsParameter(depth = 1)
3733
public List<Integer> getOptions() {
3834
return options;
3935
}

form-processing/src/main/resources/struts.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<constant name="struts.devMode" value="true" />
77
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
88
<constant name="struts.mapper.action.prefix.enabled" value="true"/>
9+
<constant name="struts.allowlist.classes" value="org.apache.struts.helloworld.model.MessageStore"/>
910

1011
<package name="basicstruts" namespace="/" extends="struts-default">
1112
<default-action-ref name="index"/>
@@ -33,7 +34,7 @@
3334
<action name="register" class="org.apache.struts.register.action.Register" method="input">
3435
<result name="input">/WEB-INF/register.jsp</result>
3536
</action>
36-
<action name="register-cancel" class="org.apache.struts.register.action.Register" method="cancel2">
37+
<action name="register-cancel">
3738
<result type="redirectAction">index</result>
3839
</action>
3940
<action name="register-submit" class="org.apache.struts.register.action.Register" method="execute2">

form-processing/src/main/webapp/WEB-INF/index.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<s:submit value="Submit" />
2121
</s:form>
2222

23-
<p><a href="register">Please register</a> for our prize drawing.</p>
23+
<p><a href="<s:url action='register'/>">Please register</a> for our prize drawing.</p>
2424
</body>
2525
</html>

form-processing/src/main/webapp/WEB-INF/register.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<body>
1010
<h3>Register for a prize by completing this form.</h3>
1111

12-
<s:form action="register-submit" namespace="test">
12+
<s:form action="register-submit" namespace="/test">
1313
<s:textfield name="personBean.firstName" label="First name"/>
1414
<s:textfield name="personBean.lastName" label="Last name"/>
1515
<s:textfield name="personBean.email" label="Email"/>

form-processing/src/main/webapp/WEB-INF/web.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
2+
<web-app id="WebApp_ID" version="6.0"
3+
xmlns="https://jakarta.ee/xml/ns/jakartaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
56
<display-name>Form_Processing_Struts2_Mvn</display-name>
67

78
<filter>

0 commit comments

Comments
 (0)