Struts2 OGNL: Resolving the Value Retrieval Issue
Introduction
Struts2, a popular web framework, utilizes Object-Graph Navigation Language (OGNL) to evaluate expressions on the web page. However, users have encountered issues retrieving values using OGNL. This article explores the root cause of the problem and presents solutions to resolve it.
The Issue
When using Struts2, users have reported difficulties in retrieving values using OGNL. The issue is characterized by a “no read method for container error” and the inability to get the value of the solution. This problem has been observed in various scenarios, including the use of Maven.
Debugging the Issue
To troubleshoot the issue, developers have employed various debugging techniques, including the use of System.out.println statements to print the values of variables. However, these efforts have been unsuccessful in resolving the issue.
Solution 1: Adding isELIgnored Attribute
One solution to this problem is to add the isELIgnored attribute to the page directive in the JSP file. This attribute is used to specify whether to ignore EL expressions on the page. By setting isELIgnored to false, users can enable EL expressions and resolve the issue.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
Solution 2: Disabling Maven
Another solution to this problem is to disable the use of Maven. By doing so, users can avoid the issue altogether.
Conclusion
In conclusion, the issue of retrieving values using OGNL in Struts2 is a common problem that can be resolved by adding the isELIgnored attribute to the page directive or disabling the use of Maven. By employing these solutions, users can successfully retrieve values using OGNL and resolve the issue.
Example Use Cases
Here are some example use cases that demonstrate the use of OGNL to retrieve values:
<%-- Password: ${password} --%>
<%-- Name 1: <%= request.getParameter("studentName")%> --%>
<%-- Name 2: ${studentName} --%>
<s:property value="[0].studentName" />
<%-- Name 4: --%>
<s:property value="[1].studentName" />
<%-- Name 5: ${requestScope.studentName} --%>
Code Snippets
Here are the code snippets used in this article:
package com.unity;
public class Student {
private String studentName;
public Student(String studentName) {
this.studentName = studentName;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = -7840227232203094654L;
private String studentName;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
private String password;
public String execute() {
System.out.println("execute .....");
System.out.println("studentName:" + studentName);
System.out.println("password:" + password);
// ValueStack vs = ActionContext.getContext().getValueStack();
ActionContext context = ActionContext.getContext();
ValueStack vs = context.getValueStack();
Student student = new Student("111111");
vs.push(student);
return "info";
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Image
This article is part of the Tencent Cloud Media-Sharing Plan, and we invite you to join and share your thoughts together.