Struts2开发步骤

1 新建一个web工程;

2把struts2核心包拷到工程的lib下;

3在web.xml中配置Struts2的前端控制器;(3拷贝)

右键项目myeclipse-add struts capabilities

选struts2.1  /* next 把勾删了 2014版本第二个勾不删

ctrl+shift+t?f  strutsp 选中 拷贝第一行路径

Web App Libraries->struts2-core-2.3.1.2.jar->org.apache.struts2.dispatcher.ng.filter->StrutsPrepareAndExecuteFilter.class

4在src目录下新建struts.xml文件;(4拷贝,跑一下)

自动生成

4新建一个Action类,完成从action到jsp功能;

package com.hw.action;

public class Hello {

public String helloWorld(){

return "hello";

}

public String add(){

return "add";

}

}

5在struts.xml里进行配置;

method="{1}">

默认为转发

另一个action type="redirectAction" (\)per_list不写Action也可以但是最好写上

/hello.jsp

/WEB-INF/user/list.jsp

/add.jsp

6编写jsp页面,接收action中传的值;

hello

add

7测试效果。

在Struts2中,一个Action类代表一次请求或调用,每个请求的动作都对应于一个相应的Action类,一个Action类是一个独立的工作单元。

在struts2中开发action有三种方式

1、写普通类,属性有set get方法,有execute()方法

struts中第 1种取值和传值方法:属性不多的情况用,

* 只要表单中的元素名和action中的属性名一致同时有set get方法即可自动取值

基本数据类型的属性对应,约定俗称:属性驱动

* JavaBean风格的属性对应,约定俗称:直接使用域对象

implements ModelDriven{//可以没有set get方法,如果有修改则需要有

private User uu=new User();

public User getModel(){//jsp不用写uu.

return uu;//要返回对象,不能为空}

2、实现 Action接口,里面自动有execute()方法,只有写属性同时有set get方法即可

com.opensymphony-xwork2

ctrl点Action Attach source 看源码 strut的都可以看

Action中有5个常量

SUCCESS = "success"

NONE = "none"

ERROR = "error"

INPUT = "input"

LOGIN = "login"

还有一个execute()方法

3、继承 ActionSupport,因为ActionSupport是在Action基础上开发的,可以直接使用Action中的属性和方法及自身特有的方法.所以在实际开发中一般使用此种方法

注:execute方法是自动调用,不是可以有2种方式调用

你可能感兴趣的:(Struts2开发步骤)