struts2_3

细说常量:

    常量可以在struts.xmlstruts.properties中配置,建议在struts.xml中配置,两种配置方式如下:

struts.xml文件中配置常量

<struts>

    <constant name="struts.action.extension" value="do"/>

</struts>

struts.properties中配置常量

struts.action.extension=do

因为常量可以在下面多个配置文件中进行定义,所以我们需要了解struts2加载常量的搜索顺序:

struts-default.xml

struts-plugin.xml

struts.xml

struts.properties

web.xml

如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.

Struts2 UI主题 :xthml simple ajax   默认的时xthml

注意:struts2spring进行集成  struts2hibernate集成

      <!– 与spring集成时,指定由spring负责action对象的创建 -->

<constant name="struts.objectFactory" value="spring" />

Struts2 默认支持 动态方法调用

动态方法调用的 常量配置:

   <!–该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -->

<constant 

name="struts.enable.DynamicMethodInvocation" value="false"/>

文件上传:

   <!--上传文件的大小限制-->  1kb

<constant name="struts.multipart.maxSize" value=1024"/>

 

接受请求参数处理:

   采用基本类型接收请求参数(get/post)

Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性。

请求路径: http://localhost:8080/test/view.action?id=78

public class ProductAction {

      private Integer id;

      public void setId(Integer id) {//struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值

             this.id = id;

      }

      public Integer getId() {return id;}

  }

采用复合类型接收请求参数

请求路径: http://localhost:8080/test/view.action?product.id=78

 public class ProductAction {

   private Product product;

   public void setProduct(Product product) {  this.product = product;  }

   public Product getProduct() {return product;}

}

Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。

自定义类型转换器:

  java.util.Date类型的属性可以接收格式为2009-07-20的请求参数值。但如果我们需要接收格式为20091221的请求参数,我们必须定义类型转换器,否则struts2无法自动完成类型转换。

import java.util.Date;

public class HelloWorldAction {

private Date createtime;

public Date getCreatetime() {

return createtime;

}

public void setCreatetime(Date createtime) {

this.createtime = createtime;

}

}

public class DateConverter extends DefaultTypeConverter {

                @Override  public Object convertValue(Map context, Object value, Class toType) {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

try { 

if(toType == Date.class){//当字符串向Date类型转换时

String[] params = (String[]) value;// Request.getParameterValues() 

return dateFormat.parse(params[0]);

}else if(toType == String.class){//Date转换成字符串时

Date date = (Date) value;

return dateFormat.format(date);

}

} catch (ParseException e) {}

return null;

}

}

将上面的类型转换器注册为局部类型转换器:

Action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassNameAction的类名,后面的-conversion.properties是固定写法,对于本例而言,文件的名称应为HelloWorldAction-conversion.properties 。在properties文件中的内容为:

属性名称=类型转换器的全类名

对于本例而言, HelloWorldAction-conversion.properties文件中的内容为:

createtime= cn.csdn.conversion.DateConverter

将上面的类型转换器注册为全局类型转换器:

WEB-INF/classes下放置xwork-conversion.properties文件 。在properties文件中的内容为:

待转换的类型=类型转换器的全类名

对于本例而言, xwork-conversion.properties文件中的内容为:

java.util.Date= cn.csdnconversion.DateConverter

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值