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

开发 后端
当Web容器收到 请求(HttpServletRequest)它将请求传递给一个标准的的过滤链包括 流程(ActionContextCleanUp)过滤器,然后经过Other filters...

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

Login.jsp:

  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

  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. }   

#p#

Struts.xml

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 
  3. <struts> 
  4.    
  5.    
  6.      <packagenamepackagename="default"extends="struts-default"> 
  7.          
  8.         <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction"> 
  9.             <resultnameresultname="success">/Success.jsp</result> 
  10.              <resultnameresultname="error">/Login.jsp</result> 
  11.                   
  12.         </action> 
  13.            
  14.            
  15.         <actionnameactionname="regAction"class="xuyan.com.action.LoginAction"method="Login"> 
  16.             <resultnameresultname="success">/RegSuccess.jsp</result> 
  17.              <resultnameresultname="error">/reg.jsp</result> 
  18.                   
  19.         </action> 
  20.            
  21.     </package> 
  22.    
  23.    
  24.    
  25.    
  26. </struts> 

Reg.jsp:

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%@ taglib uri="/struts-tags" prefix="s" %> 
  3. <
  4. String path = request.getContextPath(); 
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  6. %> 
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10.   <head> 
  11.     <base href="<%=basePath%>"> 
  12.      
  13.     <title>My JSP 'reg.jsp' starting page</title> 
  14.      
  15.     <meta http-equiv="pragma" content="no-cache"> 
  16.     <meta http-equiv="cache-control" content="no-cache"> 
  17.     <meta http-equiv="expires" content="0">     
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  19.     <meta http-equiv="description" content="This is my page"> 
  20.      
  21.  
  22.   </head> 
  23.    
  24.   <body> 
  25.       <s:form  action="regAction" method="post"> 
  26.       
  27.         <s:textfield  name="username" label="用户名" /> <br> 
  28.          
  29.          <s:password name="password"  label="密码"/> <br> 
  30.           
  31.            <input type="submit" value="注册"> <br> 
  32.       
  33.      </s:form> 
  34.   </body> 
  35. </html> 

注意:  

  1.  <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction"> 
  2.  
  3.  loginAction与login. JSP页面中的<s:form  action="loginAction" method="post">对应 
  4.  
  5.  
  6. <action name="regAction" class="xuyan.com.action.LoginAction" method="Login"> 
  7.  
  8.    regAction与reg.JSP 页面中的 <s:form  action="regAction" method="post">对应 
  9.  
  10.  
  11.    loginAction  类中public String Login()方法必须在Struts.xml中声明 
  12.  
  13.  <action name="regAction" class="xuyan.com.action.LoginAction" method="Login"> 
  14.          <result name="success">/RegSuccess.jsp</result> 
  15.           <result name="error">/reg.jsp</result> 
  16.              
  17.      </action> 

原文链接:http://blog.csdn.net/skyxuyan/article/details/8982776

责任编辑:陈四芳 来源: CSDN博客
相关推荐

2011-05-16 14:07:58

J2EE

2009-06-23 08:06:46

J2EE体系架构J2EE模型J2EE设计模式

2009-06-10 14:10:23

J2EE学习J2EE是什么

2009-06-16 11:14:00

Hibernate+SJ2EE应用开发

2009-06-19 17:03:44

J2EE学习

2009-06-10 13:37:06

J2EE可伸缩性J2EE灵活性J2EE维护

2009-06-11 17:06:11

J2EE历史Java EE概述

2009-06-19 17:29:12

Struts常见错误J2EE

2009-06-23 16:48:26

J2EE常见问题J2EE平台

2011-11-25 14:59:36

JavaJ2EE框架

2009-06-22 17:34:40

J2EE架构

2009-06-18 15:54:57

J2EE下使用JNDI

2009-06-23 08:12:48

J2EE调用存储过程

2009-06-22 16:21:02

J2EE线程

2009-06-18 16:13:14

J2EE开发

2009-06-22 17:05:41

Java EEJava企业应用

2009-06-08 21:34:09

J2EEJ2SEJ2ME

2009-07-09 16:06:10

JDK J2EE J2

2009-02-23 11:18:06

J2EE架构师Java

2009-06-11 17:19:47

J2EE设计模式Template
点赞
收藏

51CTO技术栈公众号