您的位置:首页 > 编程语言 > Java开发

Struts2.xml文件中的常量、全局错误的配置及相关动态方法的调用

2015-10-25 19:16 621 查看
问题!Struts2.xml文件中的常量有哪些,Struts2的处理过程,如何自动更新配置文件、全局错误的配置及相关动态方法的调用和方式,表单请求参数数据通过拦截器如何到实体类

1.Struts2的处理过程



2.相关常量(在开发中有些需要注意的)





上述的配置自动更新,我们使用的devMode,因为它不仅具有打印详细错误的功能,还具有自动更新配置文件的功能。还有就是静态页面的缓存,我们开发的时候是需要关闭的,后缀名的设置,若是需要多个后缀名逗号隔开,编码不用我们设置,默认请求参数为UTF-8格式。最后就是objectFactory,这个在整合ssh框架时才会用到。

3、全局错误的配置

有两种方式:

(1):单个包中的设置

<!-- package的继承案例:配置全局的结果视图 ,在下面这个例子中就有多个action被全局global控制-->

<package name="scope" namespace="/scope" extends="mypackage">
<global-results>
<result type="dispatcher" name="error">/error.jsp</result>
</global-results>
<action name="scopeAction" class="cn.itcast.action.ScopeAction" method="execute">
<result type="redirect" name="success">/scope.jsp</result>
</action>
<action name="scopeAction" class="cn.itcast.action.ScopeAction" method="execute">
<result type="redirect" name="success">/scope1.jsp</result>
</action>
</package>


(2)多个包的全局设置

<!-- 配置所有包的全局错误结果 :范围整个struts.xml文件,采用的是继承包的方式-->
<package name="mypackage" extends="struts-default">
<global-results>
<result type="dispatcher" name="error">/error.jsp</result>
</global-results>

</package>

<include file="customer.xml"></include><!--这个采用分布式集成管理struts.xml,在开发中主要是利于查看和方便管理-->

<package name="orders" namespace="/orders" extends="mypackage">
<action name="orders" class="cn.itcast.action.OrdersAction" method="add">
<result type="dispatcher" name="success">/2.jsp</result>
<result type="dispatcher" name="input">/1.jsp</result>
</action>
</package>


4、相关动态方法的调用和方式



该常量主要是对动态方法的设置,默认是开启的,开发中我们需要关闭这个常量设置

<package name="scope" namespace="/scope" extends="mypackage">
<action name="scopeAction" class="cn.itcast.action.ScopeAction" method="execute1">
<result type="redirect" name="success">/scope.jsp</result>
</action>
</package>


就好比上面这个例子,假设只写execute1方法,而这个类中有另外一个execut2方法,只是在这里并没有调用它,但是因为走到action时就实例化了,我们可以采用以下方式来调用第二个方法,相对来说不安全。



而关闭了该方式动态方法的设置:再去以同样的方式去访问它就会出以下错误(在这个namespace包空间下找不到该方法)。



试用通配符来调用在action中实例化而又没有写的方法(这样方便了很多,我们不需要去写很多方法,而同时可以访问)。



5、表单请求参数数据通过拦截器到实体类(案例说明)



package cn.itcast.action;

import java.io.Serializable;

import com.opensymphony.xwork2.ActionSupport;

public class AddressAction extends ActionSupport implements Serializable {
private Integer id;
private String city;
private String province;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}

}


package cn.itcast.action;

import java.io.Serializable;
import java.util.Date;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
//(不建议使用)动态方法调用:http://localhost:8080/struts2day02/customer/addCustomer!updateCustomer(应该执行addCustomer,使用!updateCustomer,在请求addCustomer就执行了updateCustomer)
//关闭动态调用的功能:struts.enable.DynamicMethodInvocation = false
public class CustomerAction extends ActionSupport implements Serializable{
//每次请求都会重新实例化该类,是线程安全的。
//	public CustomerAction(){
//		System.out.println("实例化了");
//	}
//

private Integer id;//Struts2会自动类型转换:仅限基本类型
private String name;
private String[] hobby;
private Date birthday;

private AddressAction address;//Struts2设置值时,如果发现该类没有实例,则创建实例(通过反射调用默认构造方法)

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public AddressAction getAddress() {
return address;
}
public void setAddress(AddressAction address) {
this.address = address;
}

public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String addCustomer(){

try{
System.out.println("addCustomer");
System.out.println("ID:"+id+"\tNAME:"+name+"\tProvince:"+address.getProvince()+"\tCITY:"+address.getCity());
System.out.println("爱好:");
for(String s:hobby){
System.out.println(s);
}
System.out.println(birthday);
ActionContext.getContext().put("birthday", birthday);
//调用service层保存用户的信息
return SUCCESS;
}catch(Exception e){
return ERROR;
}
}
public String updateCustomer(){
System.out.println("updateCustomer");
try{
//调用service层更新用户的信息
return SUCCESS;
}catch(Exception e){
return ERROR;
}
}
}


<package name="customer" namespace="/customer" extends="struts-default">
<action name="updateCustomer" class="cn.itcast.action.CustomerAction" method="addCustomer">
<result type="dispatcher" name="success">/customer/success.jsp</result>
</action>
</package>


这个例子就可以说明。我们封装数据到实体类,不需要再配置文件中写什么,只需要保证,实体类中的setter方法名字和表单的名字相同就可以了。还有就是接下来我们要注意的自动类型转化了。这个在封装数据中很重要,请查看博客:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: