Struts2环境配置及测试实例

一、下载。

二、创建web项目,导入jar包。

  1.1 解压下载包,找到apps目录下的struts2-blank.war文件,复制出来并修改后缀名为zip;

  1.2 解压struts2-blank.zip;

  1.3 找到WEB-INF目录下lib文件夹,导入目录下所有jar包到新web项目;

三、配置web.xml文件

  在WEB-INF目录下找到web.xml文件,将其中的<filter>和<filter-mapping>标签及其内容复制到项目中的web.xml内。

四、配置dtd文件

  配置dtd文件以使xml支持struts2自动补全(alt+/)。

  在Myeclipse中菜单栏window--preference--搜索xml--xml catalog--add--file system选择dtd文件。

  dtd文件目录:解压包下src\core\src\main\resources.

五、编写struts.xml文件

  在web项目的src根目录下新建一个struts.xml文件。

六、测试实例:登录

     1.  编写jsp页面。

  1.1 Login.jsp

<body>
<form action="/sys/LoginAction.action" >
用户名:<input type="text"  name="username"><br>
密码<input type="text" name="pwd"><br>
<input type="submit" value="提交">
</form>
</body>

   1.2 success.jsp

<body>
    success.
</body>

 1.3 error.jsp

<body>
  error.
</body>

  2. 编写LoginAction类>。

package struts;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
//继承ActionSupport类以直接使用SUCCESS, LOGIN等变量和重写execute等方法
//声明用户名和密码的实例变量; public String username; public int pwd; //需要在xml里调用的Login方法(返回值为String): public String Login(){ System.out.println(username+" "+pwd); if(username.trim().equals("aaa")&&pwd==123){ return "success"; }else{ return "error"; } } //系统生成的get和set方法 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getPwd() { return pwd; } public void setPwd(int pwd) { this.pwd = pwd; } }

5.3 配置struts.xml文件。

<struts>
    <package  namespace="/sys" extends="struts-default" name="default">
    <action name="LoginAction" method="Login" class="struts.LoginAction">
    <result name="success">/success.jsp</result>
    <result name="error">/error.jsp</result>
    </action>
    </package>
</struts>
posted @ 2015-09-01 10:12  SLiris  阅读(1123)  评论(0)    收藏  举报