最近,我正在学习一个关于Struts2 UI标签的教程。所以,我找到了这个例子,并完美地执行了它。
但是,在struts.xml配置文件中,我无法理解一些OGNL表达式。我在这里写道:
<struts>
<package name="default" extends="struts-default">
<action name="*Register" method="{1}" class="nirmal.RegisterAction">
<result name="populate">/register.jsp</result>
<result name="input">/register.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>在这里,我从populateRegier上填充来自index.jsp的一个请求,因此它将它重定向到类的RegisterAction.java并执行index.jsp()方法,即如下所示:
RegisterAction.java
package nirmal;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String userName;
private String password;
private String gender;
private String about;
private String country;
private ArrayList<Country> countryList;
private String[] community;
private ArrayList<String> communityList;
private Boolean mailingList;
public String populate() {
countryList = new ArrayList<Country>();
countryList.add(new Country(1, "India"));
countryList.add(new Country(2, "USA"));
countryList.add(new Country(3, "France"));
communityList = new ArrayList<String>();
communityList.add("Java");
communityList.add(".Net");
communityList.add("SOA");
community = new String[]{"Java",".Net"};
mailingList = true;
return "populate";
}
public String execute() {
return SUCCESS;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public ArrayList<Country> getCountryList() {
return countryList;
}
public void setCountryList(ArrayList<Country> countryList) {
this.countryList = countryList;
}
public String[] getCommunity() {
return community;
}
public void setCommunity(String[] community) {
this.community = community;
}
public ArrayList<String> getCommunityList() {
return communityList;
}
public void setCommunityList(ArrayList<String> communityList) {
this.communityList = communityList;
}
public Boolean getMailingList() {
return mailingList;
}
public void setMailingList(Boolean mailingList) {
this.mailingList = mailingList;
}
}第一个问题:,我不明白为什么要在这里执行()方法?
第二个问题:在struts2.xml中method="{1}"的含义是什么?
提前谢谢..。
发布于 2010-07-15 12:04:24
这两个问题的答案是相同的。如果您查看struts配置中的这一行:
<action name="*Register" method="{1}" class="nirmal.RegisterAction">您将注意到一个*和{1}。struts所做的是接受您的populateRegister请求并在上面执行通配符匹配。
它接受通配符匹配部分(填充),并使用它作为方法名(用填充替换{1} )。这就是在nirmal.RegisterAction类中调用in ()方法的原因。
如果您想在同一个类中调用execute()方法,您将发送一个executeRegister请求。在struts站点上有更多关于通配符映射的信息。就我个人而言,我发现它们对于保持配置干净非常有用。
发布于 2011-02-22 10:38:53
之所以调用填充方法,是因为您需要自动填充一些数据,这有助于用户选择或查看它,这也有助于您在默认选择中使用它。
https://stackoverflow.com/questions/3236566
复制相似问题