Struts 2 初步入门(二)

时间:2022-07-05 21:21:13

Struts 2 动态方法调用

1.在HelloWorldAction中添加两个新的方法如下:

  

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{
public String add(){
return "add";
} public String update(){
return "update";
}
@Override
public String execute() throws Exception {
System.out.println("执行excute()");
return SUCCESS; }
} 2.创建add.jsp和update.jsp
3.在strut.xml中动态配置动态方法调用如下:
  
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="hello_*" method="{1}" class ="com.imooc.action.HelloWorldAction">
<result>/result.jsp</result>
<result name="add">/{1}.jsp</result>
<result name="update">/{1}.jsp</result>
</action> </package> </struts> 4.在浏览器中输入访问地址:http://localhost:8080/0408/hello_update.action
http://localhost:8080/0408/hello_add.action
Struts 2 初步入门(二)