Struts2学习笔记之开发环境搭建


对于struts2现在用的是相当的多啊,,不学看来就得落伍了,记下一系列的学习笔记,供以后复习使用。

首先还是从所有程序员都认识的HelloWorld开始。

1、加入必要的开发包,这里所列的包是从struts2自带的demo中拷贝出来的。有如下的一些。




2、配置web.xml:

[html]

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.     
  11.   <filter>  
  12.         <filter-name>struts2</filter-name>  
  13.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  14.     </filter>  
  15.   
  16.     <filter-mapping>  
  17.         <filter-name>struts2</filter-name>  
  18.         <url-pattern>/*</url-pattern>  
  19.     </filter-mapping>  
  20. </web-app>  
3、编写进行逻辑处理的Action

Action可以是普通的POJO、还可以是继承ActionSupport的类、或是实现Action的类,对于继承或实现的方式,里面会有一些常量,方便作为返回值使用

[java]

  1. Action.ERROR ;  
  2. Action.INPUT ;  
  3. Action.LOGIN ;  
  4. Action.NONE ;  
  5. Action.SUCCESS ;  
编写一个简单的类实现ActionSupport

[java]

  1. package com.akwolf.helloworld.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class LoginAction extends ActionSupport {  
  6.     private static final long serialVersionUID = 1L;  
  7.   
  8.     private String username;  
  9.     private String password;  
  10.   
  11.     @Override  
  12.     public String execute() throws Exception {  
  13.         if ("akwolf".equals(username) && "123456".equals(password)) {  
  14.             return "success";  
  15.         }  
  16.   
  17.         return "error";  
  18.     }  
  19.       
  20.       
  21.   
  22.     public String getUsername() {  
  23.         return username;  
  24.     }  
  25.   
  26.     public void setUsername(String username) {  
  27.         this.username = username;  
  28.     }  
  29.   
  30.     public String getPassword() {  
  31.         return password;  
  32.     }  
  33.   
  34.     public void setPassword(String password) {  
  35.         this.password = password;  
  36.     }  
  37.   
  38. }  

4、进行struts.xml的配置

[html]

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <!-- 包机制防止重名,action也必须放在package下 ,可以继承自一个包-->  
  8.     <package name="helloworld" extends="struts-default" namespace="/">  
  9.         <!-- 具体的用来请求处理逻辑的action,并指定使用哪一个action进行处理 -->  
  10.         <action name="login" class="com.akwolf.helloworld.action.LoginAction">  
  11.             <!-- action处理后的返回结果,根据结果进行相应的视图跳转 -->  
  12.             <result name="success">/welcom.html</result>  
  13.             <result name="error">/error.html</result>  
  14.         </action>  
  15.     </package>  
  16. </struts>     
5、编写三个简单页面,进行视图层的显示。

index.jsp

[html]

  1.   <body>  
  2.         <form action="login.action" method="post">  
  3.             <table align="center">  
  4.                 <caption><h3>用户登录</h3></caption>  
  5.                 <tr>  
  6.                     <td>  
  7.                     <span style="white-space:pre">  </span>用户名:<input type="text" name="username" />  
  8.                     </td>  
  9.                 </tr>  
  10.                 <tr>  
  11.                     <td>  
  12.                     <span style="white-space:pre">  </span>密码:<input type="text" name="password"/>  
  13.                     </td>  
  14.                 </tr>  
  15.                 <tr align="center">  
  16.                     <td>  
  17.                     <span style="white-space:pre">  </span><input type="submit" value="登录"/>  
  18. <span style="white-space:pre">                        </span><input type="reset" value="重置"/>  
  19.                     </td>  
  20.                 </tr>  
  21.             </table>  
  22.         </form>  
  23.     </body>  
welocm.html

[html]

  1. <body>  
  2.     <h3>欢迎方位<a href="index.jsp">返回</a></h3>  
  3. </body>  

error.html

[html]

  1. <body>  
  2.     <h3>信息错误!!<a href="index.jsp">返回</a></h3>  
  3. </body>  
ok,部署运行,第一个struts程序搞到!!!!

相关内容