在 Struts 2 中初始化 Action 类 字段的正确方法是什么?
What is the correct way to initialize fields of Action classes in Struts 2?
我有一个 class,我用它来存储从以对象作为字段的数据库中检索到的数据。
我想在实例化 class 时初始化对象以避免 null
指针问题。
我以为我在某处读到它不应该初始化字段声明中的字段,因为它可能会给 Struts 带来问题(但我现在找不到语句),所以我正在初始化字段在构造函数中。
我的问题是:
你采用哪种方式重要吗?还是根本不应该这样做,只在实例化 class 之后才放入新对象?换句话说,我应该这样定义 class 吗:
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;
public MenuView(){
projectInfo = new ProjectInfo();
partyInfo = new PartyInfo();
requestTableInfo = new RequestTableInfo();
partyRequestInfo = new PartyRequestInfo();
}
后跟 getter 和 setter 之类的。
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo = new ProjectInfo();
private PartyInfo partyInfo = new PartyInfo();
private RequestTableInfo requestTableInfo = new RequestTableInfo();
private PartyRequestInfo partyRequestInfo = new PartyRequestInfo();
public MenuView(){ }
后跟 getter 和 setter 或像这样:
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;
public MenuView(){}
接着是 getter 和 setter,然后像这样实例化它:
MenuView menu = new MenuView();
menu.setProjectInfo(new ProjectInfo);
上述任何一种方法都可以,但这是最好的。初始化 class 的重点当然是避免引用 null
并使用首选值初始化它们,如下所示。
从那里添加 getter 和 setter 就可以正常工作了
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;
public MenuView(int a, int b, int c, int d){
projectInfo = new ProjectInfo(a);
partyInfo = new PartyInfo(b);
requestTableInfo = new RequestTableInfo(c);
partyRequestInfo = new PartyRequestInfo(d);
}
这对 Struts2 无关紧要,只需要您自己创建一个 ModelDriven
对象(如果您使用 ModelDriven
interface)。
提交表单时,如果对象是 null
,框架将创建对象。默认情况下启用此选项。 params
interceptor which is using OGNL 幕后执行魔术,根据传递给操作的参数填充模型。
While this interceptor is being invoked, a flag
(ReflectionContextState#CREATE_NULL_OBJECTS
) is turned on to ensure
that any null reference is automatically created - if possible. See
the type conversion documentation and the InstantiatingNullHandler
javadocs for more information.
此功能或多或少地记录在 com.opensymphony.xwork2.conversion.NullHandler 下。
bean 应符合 JavaBeans spec., so they could be instantiated by the Struts2 framework (if you want to learn more about JavaBeans, see this post What is a JavaBean exactly).
I read somewhere that you should not initialize the fields in the field declaration because that may cause problems for Struts (but I can't find the statement now)
不,我不知道
so I am initializing the fields in the constructor.
你可以,但你不是被迫的。我从不在操作上使用构造函数(我几乎从不使用构造函数,因为我使用的是 Java EE 6+ 和 CDI,并且在构造函数中还没有注入 @Inject
ed 对象 - I use a @PostConstruct
method 相反,必要时),但这取决于您,这不是规则。
My question is does it matter which way you do it?
没有
Or should you not do it at all and only put in the new objects after you instantiate the class?
Struts2 将为您处理 JSP 中的空值。您必须处理的唯一 NullPointerException
位于 Java 端,因此只需检查 null,或实例化声明中的对象,不再担心。
记住 Struts2 will need a no-arg constructor to instantiate beans with JSP values.
我有一个 class,我用它来存储从以对象作为字段的数据库中检索到的数据。
我想在实例化 class 时初始化对象以避免 null
指针问题。
我以为我在某处读到它不应该初始化字段声明中的字段,因为它可能会给 Struts 带来问题(但我现在找不到语句),所以我正在初始化字段在构造函数中。
我的问题是:
你采用哪种方式重要吗?还是根本不应该这样做,只在实例化 class 之后才放入新对象?换句话说,我应该这样定义 class 吗:
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;
public MenuView(){
projectInfo = new ProjectInfo();
partyInfo = new PartyInfo();
requestTableInfo = new RequestTableInfo();
partyRequestInfo = new PartyRequestInfo();
}
后跟 getter 和 setter 之类的。
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo = new ProjectInfo();
private PartyInfo partyInfo = new PartyInfo();
private RequestTableInfo requestTableInfo = new RequestTableInfo();
private PartyRequestInfo partyRequestInfo = new PartyRequestInfo();
public MenuView(){ }
后跟 getter 和 setter 或像这样:
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;
public MenuView(){}
接着是 getter 和 setter,然后像这样实例化它:
MenuView menu = new MenuView();
menu.setProjectInfo(new ProjectInfo);
上述任何一种方法都可以,但这是最好的。初始化 class 的重点当然是避免引用 null
并使用首选值初始化它们,如下所示。
从那里添加 getter 和 setter 就可以正常工作了
public class MenuView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;
public MenuView(int a, int b, int c, int d){
projectInfo = new ProjectInfo(a);
partyInfo = new PartyInfo(b);
requestTableInfo = new RequestTableInfo(c);
partyRequestInfo = new PartyRequestInfo(d);
}
这对 Struts2 无关紧要,只需要您自己创建一个 ModelDriven
对象(如果您使用 ModelDriven
interface)。
提交表单时,如果对象是 null
,框架将创建对象。默认情况下启用此选项。 params
interceptor which is using OGNL 幕后执行魔术,根据传递给操作的参数填充模型。
While this interceptor is being invoked, a flag (
ReflectionContextState#CREATE_NULL_OBJECTS
) is turned on to ensure that any null reference is automatically created - if possible. See the type conversion documentation and theInstantiatingNullHandler
javadocs for more information.
此功能或多或少地记录在 com.opensymphony.xwork2.conversion.NullHandler 下。
bean 应符合 JavaBeans spec., so they could be instantiated by the Struts2 framework (if you want to learn more about JavaBeans, see this post What is a JavaBean exactly).
I read somewhere that you should not initialize the fields in the field declaration because that may cause problems for Struts (but I can't find the statement now)
不,我不知道
so I am initializing the fields in the constructor.
你可以,但你不是被迫的。我从不在操作上使用构造函数(我几乎从不使用构造函数,因为我使用的是 Java EE 6+ 和 CDI,并且在构造函数中还没有注入 @Inject
ed 对象 - I use a @PostConstruct
method 相反,必要时),但这取决于您,这不是规则。
My question is does it matter which way you do it?
没有
Or should you not do it at all and only put in the new objects after you instantiate the class?
Struts2 将为您处理 JSP 中的空值。您必须处理的唯一 NullPointerException
位于 Java 端,因此只需检查 null,或实例化声明中的对象,不再担心。
记住 Struts2 will need a no-arg constructor to instantiate beans with JSP values.