第一种配置方式
action:有add和update这两个类
public String add()
public String update()
struts.xml:有action的配置,且配置中有name是jsp访问的路径相当于servlet的名字,还有方法对应action的方法以及type
<action name="add_Product" class="com.action.ProductAction" method="add">
<result name="ok" type="redirect">${path}</result>
</action>
<action name="update_Product" class="com.action.ProductAction" method="update">
<result name="ok" type="redirect">${path}</result>
</action>
多个配置文件,不常用
action:
public class OrderAction {
public String addOrder(){
System.out.println("order");
return null;
}
}
public class UserAction {
public String addUser(){
System.out.println("user");
return null;
}
}
struts.xml:
<struts>
<constant name="struts.ui.theme" value="simple"></constant>
<include file="struts-order.xml"></include>
<include file="struts-user.xml"></include>
</struts>
struts-order.xml:
<struts>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="order" namespace="/" extends="struts-default">
<action name="Order" class="com.action.OrderAction" method="addOrder">
</action>
</package>
</struts>
struts-user.xml
<struts>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="user" namespace="/" extends="struts-default">
<action name="User" class="com.action.UserAction" method="addUser">
</action>
</package>
</struts>
多个配置文件包含到一个主配置文件中
配置优化
1、动态方法调用的优化
(1)在struts的配置文件中不用指定action的方法,只需要定义访问路径
(2)访问时使用以下方式设置路径
http://…/…/访问路径!方法名.action
action:
public String add()
public String update()
struts.xml:
<action name="product" class="com.action.ProductAction">
<result name="ok" type="redirect">${path}</result>
</action>
add.jsp
<form id="form1" name="form1" method="post" action="product!add.action">
href="product!showAll.action
但是这种优化只适合一个action优化,如果涉及到多个action就会不适用
通配符配置优化(约定式配置)
(1)约定式配置–共同约定一种路径的访问规则,例如:方法名_实体类名(Action前缀)
(2)配置方式
<action name="*_*" class=“包名” .{2}Action method="{1}">
1,第一个*表示方法名,第二个*表示Action的第一个单词名称
2、{1}对用第一个*对应的单词(方法名)
3、{2}对应第二个*对应的单词(实体类)
4、不能使用转发方式访问action,只能使用重定向
注解配置
1、使用注释的方式,设置配置,不用单独维护配置文件,可指定转发或者重定向
2、注解常用的类型
a、@Action(value=“action路径”,result={
@result(name=“方法返回值”,location=“要响应的路径”
type =“转发方式”)
。。。
})
public String 要执行的方法(){
。。。
return “返回值”
}
3、不用维护配置文件,配置文件里面没有东西
4、要访问的方法在url中写value值.action
5、转发方式默认是转发
6、配置根节点/:@namespace(“/”)
7、注意转向的时候要在path路径之前加一个/