使用Struts2创建一个登录实例

Struts2事实上并不是Struts 1的升级版,而是WebWork的升级版。因为Struts2是WebWork的升级,而不是一个全新的框架,因此稳定性、性能等各方面都有很好的保证:而且吸收了Struts 1和WebWork两者的优势,是一个好的框架,使用起来,非常的简洁,干净,强大。下面我们就用Struts2来创建一个简单的登录实例。

1、首先当然是在WEB工程里建立好Struts2框架,在这里过程中,我们需要到apache官网下载Struts2的包,我这里下好的是struts-2.1.8.1-all.zip,下好后,解压出来,把所需的commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar、freemarker-2.3.15.jar、ognl-2.7.3.jar、struts2-core-2.1.8.1.jar、struts2-pell-multipart-plugin-2.1.8.1.jar、xwork-core-2.1.6.jar这几个包拷到项目的lib下,再在web.xml中加入Struts2的过滤器,如下:

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3. xmlns="http://java.sun.com/xml/ns/j2ee"    
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.         <filter>  
  8.         <filter-name>struts2</filter-name>  
  9.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  10.     </filter>  
  11.     <filter-mapping>  
  12.         <filter-name>struts2</filter-name>  
  13.         <url-pattern>/*</url-pattern>  
  14.     </filter-mapping>  
  15.   <welcome-file-list>  
  16.     <welcome-file>index.jsp</welcome-file>  
  17.   </welcome-file-list>  
  18. </web-app>  

2、接下来我们可以开始工作了,首先写一个JSP页面

Code:
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
  2. <html>   
  3.   <head>   
  4.   <title> 登录页面 </title>   
  5.   <link rel=stylesheet href="css/login.css" type="text/css">   
  6.   </head>   
  7.   <body>   
  8.   <h1 align="center">用户登录页面</h1>   
  9.   <hr>   
  10.    <div align="center">   
  11.    <form action="login.lp" method="post">   
  12.    <table  cellspacing=5 border=5 bodercolor=#ffaa00 >   
  13.    <tr><th colspan="3" align="center"  bgcolor=#ffaa00>用户登录</th></tr>   
  14.    <tr>   
  15.    <th rowspan="3" background="images/2.jpg" style="width=90px"></th>   
  16.    <td>用户名:</td><td><input type="text" class="message" name="username"></td></tr>   
  17.    <tr><td>密  码:</td><td><input class="message" type="password" name="userpassword"></td></tr>   
  18.     <tr><td colspan="2" align="center"><input type="submit" value="登录">  <input type="reset" value="重置"></td></tr>   
  19.    </table>   
  20.    </form>   
  21.    </div>   
  22.   </body>   
  23. </html>   

3、写好JSP页面,接下来要写Struts2的action,它的action比Struts1的要简洁的多,非常的方便。如下:

Code:
  1. package myclass.struts2.action;   
  2. public class LoginAction {   
  3.     private String username;   
  4.     private String userpassword;   
  5. public String getUsername() {   
  6.         return username;   
  7.     }   
  8.     public void setUsername(String username) {   
  9.         this.username = username;   
  10.     }   
  11.   
  12.     public String getUserpassword() {   
  13.         return userpassword;   
  14.     }   
  15.   
  16.     public void setUserpassword(String userpassword) {   
  17.         this.userpassword = userpassword;   
  18.     }   
  19.   
  20. public String execute(){   
  21.     System.out.println("我进来了呢");   
  22.     System.out.println(this.getUsername());   
  23.     System.out.println(this.getUserpassword());   
  24.     if("liping".equals(this.getUsername())&"123456".equals(this.getUserpassword())){   
  25.         return "succ";   
  26.     }else{   
  27.         return "fail";   
  28.     }   
  29. }   
  30. }   

同时也写个退出系统的action吧,

Code:
  1. package myclass.struts2.action;   
  2.   
  3. public class LogoutAction {   
  4. public String execute(){   
  5.     System.out.println("退出系统");   
  6.     return "exit";   
  7.        
  8. }   
  9. }   

写好后,就可以来配置struts.xml配置文件了,这是同样与struts.1一样是核心,struts.2可以支持分开来管理不同的xml文件,所以我们把登入的action与登出的action分别配置一下,如下:

登入的:

Code:
  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.     <struts>  
  6.                      <!--使用扩展名常量,使请求的扩展名为lp,可以根据你的需要设置成任何格式-->  
  7.     <constant name="struts.action.extension" value="lp" />  
  8.     <package name="login" namespace="/" extends="struts-default">  
  9.     <action name="login" class="myclass.struts2.action.LoginAction">    
  10.     <result name="succ">/succ.jsp</result>  
  11.     <result name="fail">/failure.jsp</result>  
  12.     </action>  
  13.     </package>  
  14.     </struts>  

登出的:

Code:
  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.     <struts>  
  6.     <constant name="struts.action.extension" value="lp" />  
  7.     <package name="logout" namespace="/" extends="struts-default">  
  8.     <action name="logout" class="myclass.struts2.action.LogoutAction">    
  9.     <result name="exit">/exit.jsp</result>  
  10.     </action>  
  11.     </package>  
  12.     </struts>  

result节点配置的是action跳转的页面,通过与name的值匹配。然后把这两个xml放到struts中来:

Code:
  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.     <struts>  
  6.     <include file="login.xml"></include>  
  7.     <include file="logout.xml"></include>  
  8.     </struts>  

这样,登录的实例就做好了,接下来开启tomcat运行即可!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值