struts.xml属性配置,action属性,通配符,action接受参数(二)
2013-11-17 14:38
555 查看
尊重原创,文章出处:http://blog.csdn.net/chunlei_zhang/article/details/16369439
一、如果我们在Struts.xml中配置了studentadd和studentdelete等多个前面都是student后面是动作的action,并且各自调用动作名的方法,我们就可以使用通配符来配置action,如Struts.xml的配置如下:
等价于:
二、在action中接受参数有三种方式
1)第一种方式:如:url:http://localhost:8080/ThridProject/hello?username=zhang&password=123
或者使用form表单进行提交:
这个时候的Struts.xml的配置为:
HelloAction的类的第一种实现代码如下:
url:http://localhost:8080/ThridProject/hello?user.username=zhang&user.password=123(注意变化的地方)
这种方法,model可以自己new出来,也可以通过传参来被struts自己实现,但是如果是通过传参,必须保证model有空的构造方法,否则会传参失败。(注意点)
这个时候struts.xml的配置不变,而java的后台实现需要加入model层,从action层方位model层的代码:
HelloAction的实现源码:
3)第三种方式是使用modelDriven的形式。通过继承ModelDriven的接口实现ModelDriven的getModel方法。而且不用使用set和get方法,可以自己进行new出来。
一般最常用的是使用第二种,使用DTO的形式。
三、include来包含其他的xml配置,可以实现多个人开发,开发完成后将struts.xml的配置包含进来即可。
四、default-action-ref的配置
这是默认action引用,如果别人访问这个namespace的时候,如果找不到action,则会使用默认的action。
但是default-action-ref存在bug,不会执行后面的class,这个时候我们可以使用web.xml的配置完成这个bug
一、如果我们在Struts.xml中配置了studentadd和studentdelete等多个前面都是student后面是动作的action,并且各自调用动作名的方法,我们就可以使用通配符来配置action,如Struts.xml的配置如下:
<package name="default" extends="struts-default" namespace="/"> <action name="studentadd" class="com.thridProject.action.StudentAddAction" method ="add"> <result type="dispatcher" name="success">/studentadd.jsp</result> </action> <action name ="studentdelete" class="com.thridProject.action.StudentDeleteAction" method="delete"> <result name="success">/studentdelete.jsp</result> </action> </package>
等价于:
<package name="default" extends="struts-default" namespace="/"> <action name="student*" class="com.thridProject.action.StudentAction" method ="{1}"> <result type="dispatcher" name="success">/student{1}.jsp</result> </action> </package>其中StudentAction类中含有add和delete方法。后面的{1}代表前面第几个*号,同样的action也可以使用
class="com.thridProject.action.Student{1}Action"
二、在action中接受参数有三种方式
1)第一种方式:如:url:http://localhost:8080/ThridProject/hello?username=zhang&password=123
或者使用form表单进行提交:
这个时候的Struts.xml的配置为:
<package name="default" extends="struts-default" namespace="/"> <action name="hello" class="com.thridProject.action.HelloAction" method="add"> <result type="dispatcher" name="success">/hello.jsp</result> </action> </package>
HelloAction的类的第一种实现代码如下:
package com.thridProject.action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport { /** * author 张春蕾 2013-11-17 */ private static final long serialVersionUID = 1L; private String username; private String password; 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 add(){ System.out.println("username = "+username); System.out.println("password = "+password); return "success"; } }输出结果为:
username = zhang password = 1232)第二种方式使用ThemainModel的形式,因为提交的参数肯定会封装成一个对象,然后对数据库进行访问。
url:http://localhost:8080/ThridProject/hello?user.username=zhang&user.password=123(注意变化的地方)
这种方法,model可以自己new出来,也可以通过传参来被struts自己实现,但是如果是通过传参,必须保证model有空的构造方法,否则会传参失败。(注意点)
这个时候struts.xml的配置不变,而java的后台实现需要加入model层,从action层方位model层的代码:
HelloAction的实现源码:
package com.thridProject.action; import com.opensymphony.xwork2.ActionSupport; import com.thridProject.model.User; public class HelloAction extends ActionSupport { /** * author 张春蕾 2013-11-17 */ private static final long serialVersionUID = 1L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String add(){ System.out.println("username = "+user.getUsername()); System.out.println("password = "+user.getPassword()); return "success"; } }model层的实现源码:
package com.thridProject.model; /** * author 张春蕾 2013年11-17 */ public class User { private String username; private String password; 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; } }
3)第三种方式是使用modelDriven的形式。通过继承ModelDriven的接口实现ModelDriven的getModel方法。而且不用使用set和get方法,可以自己进行new出来。
一般最常用的是使用第二种,使用DTO的形式。
三、include来包含其他的xml配置,可以实现多个人开发,开发完成后将struts.xml的配置包含进来即可。
<include file="XX.xml"/>
四、default-action-ref的配置
<package name="default" extends="struts-default" namespace="/"> <default-action-ref name="hello"></default-action-ref> <action name="hello" class="com.thridProject.action.HelloAction"> <result name="success">/hello.jsp</result> </action> </package>
这是默认action引用,如果别人访问这个namespace的时候,如果找不到action,则会使用默认的action。
但是default-action-ref存在bug,不会执行后面的class,这个时候我们可以使用web.xml的配置完成这个bug
相关文章推荐
- Struts2-Action接受参数方式、method属性使用及通配符的配置
- Struts2学习笔记之与struts.xml配置和Action有关的属性
- 动态结果配置:struts.xml中,<result>属性的type配置之redirectAction(请求重定向到指定的Action)
- struts-config.xml中配置action的input属性的作用
- 3.Struts2 struts.xml配置和Action有关的属性
- Structs 在Struts.xml中配置action时,action的name属性最好首字母大写
- Struts2 两个Action 动态传参数,在struts.xml中配置
- struts.xml文件中 result标签的 type 属性为redirectAction时URL参数与值的获得
- Struts2学习笔记之与struts.xml配置和Action有关的属性
- struts2中struts.xml中的action标签的method属性配置为"{1}"的时候是什么意味
- struts.xml 使用 type="redirect" 和type="redirectaction" 传递参数
- struts.xml配置文件(package,namespace,action)
- Struts Action 之 配置属性及其含义
- struts配置文件path属性传多个参数时注意点
- 在struts配置文件中action元素包含哪些属性和子元素?
- struts2 接受参数的两种方式(属性驱动和模型驱动)
- 阿翔编程学-struts-config.xml中action元素的attribute属性的意义
- struts2.0中spring配置文件中action的属性 scope="prototype" .
- Struts2 方法动态调用,验证表签,xml配置Action
- struts.xml中Action的配置详解