欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

J2EE学习笔记——Struts2多方法实现(1)

来源: javaer 分享于  点击 4090 次 点评:174

J2EE学习笔记——Struts2多方法实现(1)


实现  登陆 验证 和注册 验证在一个  LoginAction  类中:

Login.jsp:

[html] view plaincopy
  1. <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> 
  2. <%@ taglib uri="/struts-tags"prefix="s" %> 
  3.    
  4. <%   
  5. String path = request.getContextPath();   
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  7. %> 
  8.    
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  10. <html> 
  11.   <head> 
  12.     <basehrefbasehref="<%=basePath%>"> 
  13.        
  14.     <title>Login</title> 
  15.     <metahttp-equivmetahttp-equiv="pragma"content="no-cache"> 
  16.     <metahttp-equivmetahttp-equiv="cache-control"content="no-cache"> 
  17.     <metahttp-equivmetahttp-equiv="expires"content="0"> 
  18.     <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> 
  19.     <metahttp-equivmetahttp-equiv="description"content="This is my page"> 
  20.    
  21.   </head> 
  22.      
  23.   <body> 
  24.        
  25.      <s:formactions:formaction="loginAction"method="post"> 
  26.         
  27.         <s:textfieldnames:textfieldname="username"label="用户名"/><br> 
  28.            
  29.          <s:passwordnames:passwordname="password"label="密码"/><br> 
  30.             
  31.            <inputtypeinputtype="submit"value="登陆"><br> 
  32.         
  33.      </s:form> 
  34.        
  35.        
  36.   </body> 
  37. </html> 

LoginAction

[java] view plaincopy
  1. package xuyan.com.action;   
  2.    
  3. import java.sql.Connection;   
  4. import java.sql.PreparedStatement;   
  5. import java.sql.ResultSet;   
  6. import java.sql.SQLException;   
  7.    
  8. import xuyan.com.model.User;   
  9. import xuyan.com.model.UserDAO;   
  10.    
  11. import com.opensymphony.xwork2.ActionSupport;   
  12. import com.opensymphony.xwork2.ModelDriven;   
  13.    
  14. publicclass LoginAction  extends ActionSupport implements ModelDriven<User>{   
  15.    
  16.        
  17.     /**  
  18.      *   
  19.      */ 
  20.     privatestaticfinallong serialVersionUID = 1L;   
  21.        
  22.      User user=new User();   
  23.    
  24.     public User getUser() {   
  25.         return user;   
  26.     }   
  27.     publicvoid setUser(User user) {   
  28.         this.user = user;   
  29.     }   
  30.     private Connection con=null;   
  31.     private ResultSet rs=null;   
  32.     private PreparedStatement psmt=null;   

  33.        
  34.     /**  
  35.      * 用户注册  
  36.      *   
  37.      */ 
  38.         
  39.     public String Login()     
  40.     {   
  41.         System.out.println(user.getUsername()+"1111");   
  42.         System.out.println(user.getPassword()+"1111");            
  43.            
  44.         UserDAO dao=new UserDAO();   
  45.            
  46.         con=dao.getConnection();   
  47.            
  48.         try {   
  49.             psmt =con.prepareStatement("insert into  userinfo (username,password) values (?,?) ");   
  50.             psmt.setString(1, user.getUsername());   
  51.             psmt.setString(2, user.getPassword());   
  52.                
  53.             int a=psmt.executeUpdate();   
  54.                
  55.             if(a>0)   
  56.             {   
  57.                 System.out.println(user.getUsername()+"第2222次");   
  58.                 System.out.println(user.getPassword()+"第2222次");   
  59.                    
  60.                 return  SUCCESS;   
  61.             }   
  62.             else 
  63.             {   
  64.                 return  ERROR;   
  65.             }   
  66.                
  67.         } catch (SQLException e) {   
  68.                
  69.             e.printStackTrace();   
  70.             return  ERROR;   
  71.         }   
  72.            
  73.         finally 
  74.         {   
  75.             if(con != null){   
  76.                 try {   
  77.                     con.close();   
  78.                 } catch (SQLException e) {   
  79.                     e.printStackTrace();   
  80.                 }   
  81.             }   
  82.             if(psmt != null){   
  83.                 try {   
  84.                     psmt.close();   
  85.                 } catch (SQLException e) {   
  86.                     e.printStackTrace();   
  87.                 }   
  88.             }   
  89.             if(rs != null){   
  90.                 try {   
  91.                     rs.close();   
  92.                 } catch (SQLException e) {   
  93.                     e.printStackTrace();   
  94.                 }   
  95.             }   
  96.         }   
  97.            
  98.     }   
  99.             
  100.     @Override 
  101.     public String execute()  {   
  102.            
  103.         System.out.println(user.getUsername()+"1111");   
  104.         System.out.println(user.getPassword()+"1111");   
  105.            
  106.            
  107.         UserDAO dao=new UserDAO();   
  108.            
  109.         con=dao.getConnection();   
  110.            
  111.         try {   
  112.             psmt =con.prepareStatement("select * from userinfo where username=? and password=?");   
  113.             psmt.setString(1, user.getUsername());   
  114.             psmt.setString(2, user.getPassword());   
  115.                
  116.             rs=psmt.executeQuery();   
  117.                
  118.             if(rs.next())   
  119.             {   
  120.                 System.out.println(user.getUsername()+"第2222次");   
  121.                 System.out.println(user.getPassword()+"第2222次");   
  122.                    
  123.                 return  SUCCESS;   
  124.             }   
  125.             else 
  126.             {   
  127.                 return  ERROR;   
  128.             }   
  129.                
  130.         } catch (SQLException e) {   
  131.                
  132.             e.printStackTrace();   
  133.             return  ERROR;   
  134.         }   
  135.            
  136.         finally 
  137.         {   
  138.             if(con != null){   
  139.                 try {   
  140.                     con.close();   
  141.                 } catch (SQLException e) {   
  142.                     e.printStackTrace();   
  143.                 }   
  144.             }   
  145.             if(psmt != null){   
  146.                 try {   
  147.                     psmt.close();   
  148.                 } catch (SQLException e) {   
  149.                     e.printStackTrace();   
  150.                 }   
  151.             }   
  152.             if(rs != null){   
  153.                 try {   
  154.                     rs.close();   
  155.                 } catch (SQLException e) {   
  156.                     e.printStackTrace();   
  157.                 }   
  158.             }   
  159.         }   
  160.            
  161.            
  162.     }   
  163.     public User getModel() {   
  164.         // TODO Auto-generated method stub 
  165.         return user;   
  166.     }   
  167. }   


相关栏目:

用户点评