11级_Java_曹建波 02.28 Struts2_第三天
复习:
1、什么是struts2
2、怎么搭建struts2开发环境
1>>下载struts2所需要的jar文件
首页上就可以直接点击下载需要的最新版本。
Tomcat删除掉。
2>>添加struts2的配置文件struts.xml---dtd-->核心的jar文件中可以找到dtd文件
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
3>>在web.xml文件中添加启动struts2MVC框架的过滤器
加载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.jar的org.apache.strut2.default.properties
文件中找到后缀为.action的配置常量struts.action.extension=action,,
能不能修改?在哪里修改?
struts.xml
struts.properties
细说常量:
常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下:
在struts.xml文件中配置常量
<struts>
<constantname="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
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.
Struts2UI主题:xthmlsimpleajax默认的时xthml
注意:struts2与spring进行集成struts2与hibernate集成
<!–与spring集成时,指定由spring负责action对象的创建-->
<constantname="struts.objectFactory"value="spring"/>
Struts2默认支持动态方法调用
动态方法调用的常量配置:
<!–该属性设置Struts2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。-->
<constant
name="struts.enable.DynamicMethodInvocation"value="false"/>
文件上传:
<!--上传文件的大小限制-->1kb
<constantname="struts.multipart.maxSize"value=“1024"/>
动态方法的调用呢?
http://localhost:8080/struts_dynamic/****/delete!adds.action
action中有一个
publicStringdelete(){
System.out.println(“用户删除……操作”);
returnSUCCESS;
}
struts.xml文件中
http://localhost:8080/struts_dynamic/
****/action名称!方法名称.action
使用通配符:
在struts.xml文中
必须理解:
第一个*代表的是类名
第二个*代表的是方法名
意思:用这样的一个配置就可以搞定www.****.struts_dynamic.action这个包中所有的action处理
这样会导致
<resultname=”会有n个名称”>
接受请求参数处理:
采用基本类型接收请求参数(get/post)
在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性。
请求路径:http://localhost:8080/test/view.action?id=78
publicclassProductAction{
privateIntegerid;
publicvoidsetId(Integerid){//struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值
this.id=id;
}
publicIntegergetId(){returnid;}
}
采用复合类型接收请求参数
请求路径:http://localhost:8080/test/view.action?product.id=78
publicclassProductAction{
privateProductproduct;
publicvoidsetProduct(Productproduct){this.product=product;}
publicProductgetProduct(){returnproduct;}
}
Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。
自定义类型转换器:
java.util.Date类型的属性可以接收格式为2009-07-20的请求参数值。但如果我们需要接收格式为20091221的请求参数,我们必须定义类型转换器,否则struts2无法自动完成类型转换。
importjava.util.Date;
publicclassHelloWorldAction{
privateDatecreatetime;
publicDategetCreatetime(){
returncreatetime;
}
publicvoidsetCreatetime(Datecreatetime){
this.createtime=createtime;
}
}
publicclassDateConverterextendsDefaultTypeConverter{
@OverridepublicObjectconvertValue(Mapcontext,Objectvalue,ClasstoType){
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyyMMdd");
try{
if(toType==Date.class){//当字符串向Date类型转换时
String[]params=(String[])value;//Request.getParameterValues()
returndateFormat.parse(params[0]);
}elseif(toType==String.class){//当Date转换成字符串时
Datedate=(Date)value;
returndateFormat.format(date);
}
}catch(ParseExceptione){}
returnnull;
}
}
将上面的类型转换器注册为局部类型转换器:
在Action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是Action的类名,后面的-conversion.properties是固定写法,对于本例而言,文件的名称应为HelloWorldAction-conversion.properties。在properties文件中的内容为:
属性名称=类型转换器的全类名
对于本例而言,HelloWorldAction-conversion.properties文件中的内容为:
createtime=cn.****.conversion.DateConverter
将上面的类型转换器注册为全局类型转换器:
在WEB-INF/classes下放置xwork-conversion.properties文件。在properties文件中的内容为:
待转换的类型=类型转换器的全类名
对于本例而言,xwork-conversion.properties文件中的内容为:
java.util.Date=cn.****conversion.DateConverter