I am using struts2.3.24 and trying to setup a demo that OGNL calls a call constructor
package ac.nz.unitec.struts2;
public class User {
private String username;
private int age;
public User(String username, int age){
this.username = username;
this.age = age;
}
public User(int age){
this.age = age;
System.out.println("user");
}
public User(){}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "user " + age;
}
}
in JSP, I try to cal User constructor:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>OGNL</title>
</head>
<body>
zz
<s:property value="new ac.nz.unitec.struts2.User(28)"/><br>
zz
<s:debug/>
</body>
</html>
However, the output only shows zz and zz, I don't figure out what's wrong with it. Thanks for any suggestion
In the latest versions of Struts2 creating objects directly in JSP is NOT allowed due to security reasons.
Create a method inside your action which creates a new object and call it from JSP or you can try to use <s:bean>
tag.
Read more about Internal security mechanism.