Struts2第三天总结

复习:

1、什么是struts2

2、怎么搭建struts2开发环境

  1>> 下载struts2所需要的jar文件

       http://struts.apache.org

      首页上就可以直接点击下载需要的最新版本。


Tomcat 删除掉。

 

 2>>添加struts2的配置文件   struts.xml ---dtd-->核心的jar文件中 可以找到 dtd文件

 <!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

3>>web.xml文件中 添加启动struts2 MVC框架的过滤器 

       加载struts2的配置文件   面试题

         struts-default.xml

         struts-plugin.xml

         struts.xml

         struts.properties

         web.xml

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

struts2的常量

    常见的struts常量

        http://localhost:8080/struts_action/login.action

    为什么是.action  并且.action能不能修改面试题

struts2-core-2.3.8.jarorg.apache.strut2.default.properties
     

文件中找到后缀为.action的配置常量 struts.action.extension=action,,

能不能修改?在哪里修改?

 struts.xml

 struts.properties

细说常量:

    常量可以在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"/>

动态方法的调用呢?

    

   http://localhost:8080/struts_dynamic/csdn/delete!adds.action

   action中有一个

             

   struts.xml文件中

          

 http://localhost:8080/struts_dynamic/

  csdn/action名称!方法名称.action

使用通配符 :


  在struts.xml文中

   

地址栏: http://localhost:8080/struts_dynamic/csdn/user

http://localhost:8080/struts_dynamic/csdn/user_add

 找这个Action  UserAction 中的

必须理解:

 

第一个*代表的是类名

第二个*代表的是方法名

意思:用这样的一个配置 就可以搞定 www.csdn.struts_dynamic.action这个包中所有的action处理

这样会导致   

<result name=”会有n个名称”>

接受请求参数处理:

   采用基本类型接收请求参数(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

posted @ 2013-02-28 19:28  流-星-追-月  阅读(126)  评论(0编辑  收藏  举报