编写动作类的三种方式
1.动作类的第一种:普通的javaBean,没有任何继承 POJO(Plan Old Java Object)
Action类:
/**
* Created by dllo on 17/7/4.
* 动作类的第一种
* POJO:Plan Old Java Object
*/
public class DemoAction1 {
//动作方法:
public String demo1(){
return "success";
}
}
struts.xml
<struts>
<package name="actions" extends="struts-default">
<action name="demo1" class="com.wangyjie.struts.actions.DemoAction1" method="demo1">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
2. 动作类第二种 实现Action接口
![这里写图片描述](https://img-blog.csdn.net/20170708115946011?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFweWFuZHhweQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Action类
/**
* Created by dllo on 17/7/4.
* 动作类的第二种
*
* 实现Action的接口
*/
public class DemoAction2 implements Action{
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
struts.xml
<package name="action2" extends="struts-default">
<action name="demo2" class="com.wangyjie.struts.actions.DemoAction2" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
3. 动作类第三种:继承ActionSupport[推荐使用]
//Action类
/**
* Created by dllo on 17/7/4.
* 动作类的第三种: [推荐使用]
* 继承于ActionSupport
*/
public class DemoAction3 extends ActionSupport{
public String www(){
return ERROR;
}
}
//struts.xml
<package name="action3" extends="struts-default">
<action name="demo3" class="com.wangyjie.struts.actions.DemoAction3" method="www">
<result name="error">/error.jsp</result>
</action>
</package>
动作类中的动作方法编写要求
public String XXX(){}
使用通配符配置action
在配置元素时,允许在指定name属性时,使用模式字符串(“*”代表一个或多个字符)
为了防止写一些这样重复性的东西 出现了通配符
<action name="delete_Customer" class="com.wangyjie.struts.wildcard.CustomerAction" method="deleteCustomer">
<result name="success">/deleteCustomer.jsp</result>
</action>
<action name="edit_Customer" class="com.wangyjie.struts.wildcard.CustomerAction" method="editCustomer">
<result name="success">/editCustomer.jsp</result>
</action>
<action name="query_Customer" class="com.wangyjie.struts.wildcard.CustomerAction" method="queryCustomer">
<result name="success">/queryCustomer.jsp</result>
<!--通配符 类似我们之前学过的占位符一样-->
<action name="*Customer" class="com.wangyjie.struts.wildcard.CustomerAction" method="{1}Customer">
<result name="success">{1}Customer.jsp</result>
</action>
<action name="*_*" class="com.wangyjie.struts.wildcard.{2}Action" method="{1}{2}">
<result name="success">{1}{2}.jsp</result>
</action>
Action访问ServletAPI
方式一:ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
实现ServletRequestAware接口
private HttpServletRequest request;
//重写方法
@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = httpServletRequest;
}
public HttpServletRequest getRequest(){
return request;
}