程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> struts框架實現登錄示例

struts框架實現登錄示例

編輯:關於JSP

1、新建一個javaweb項目,並將struts的jar包拷貝到WebRoot/WEB-INF/lib下。

                  \
 


2、拷貝完jar包還不能進行工作,用戶的請求必須達到ActionServlet,我們才能通過struts處理請求,所以還需要將ActionServlet配置在web.xml文件裡面。


[java]
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4"  
    xmlns="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  <servlet> 
    <servlet-name>action</servlet-name> 
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>action</servlet-name> 
    <url-pattern>*.do</url-pattern> 
  </servlet-mapping> 
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

 


3、建立actionForm,表單上的用戶名和密碼,必須與actionForm中屬性的get/set方法後半部分一致。


[java] 
package com.xxjstgb.struts; 
import org.apache.struts.action.ActionForm; 
/**
 * 登錄ActionForm,負責表單收集數據
 * @author liuzhengquan
 *
 */@SuppressWarnings("serial") 
public class LoginActionForm extends ActionForm { 
     private String userName; 
     private String password; 
    public String getUserName() { 
        return userName; 
    } 
    public void setUserName(String userName) { 
        this.userName = userName; 
    } 
    public String getPassword() { 
        return password; 
    } 
    public void setPassword(String password) { 
        this.password = password; 
    } 

package com.xxjstgb.struts;
import org.apache.struts.action.ActionForm;
/**
 * 登錄ActionForm,負責表單收集數據
 * @author liuzhengquan
 *
 */@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm {
  private String userName;
  private String password;
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
}

 


4、建立LoginAction,這裡需要繼承struts的Action類。處理Model層的邏輯,並返回轉向信息。


[java] 
package com.xxjstgb.struts; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.struts.action.Action; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 
/**
 * 登錄Action
 * 負責取得表單數據、調用業務邏輯、返回轉向信息
 * @author liuzhengquan
 *
 */ 
public class LoginAction extends Action { 
 
    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) 
            throws Exception { 
        LoginActionForm laf=(LoginActionForm)form; 
        String userName=laf.getUserName(); 
        String password=laf.getPassword(); 
        UserManager userManager=new UserManager(); 
        try { 
            userManager.login(userName, password); 
             
            return mapping.findForward("success"); 
        } catch (UserNoFoundException e) { 
            e.printStackTrace(); 
            request.setAttribute("msg", "用戶不能找到,用戶名稱=【"+userName+"】"); 
        }catch (PasswordErrorException e) { 
            e.printStackTrace(); 
            request.setAttribute("msg", "密碼錯誤"); 
        } 
        return mapping.findForward("error"); 
    } 

package com.xxjstgb.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
 * 登錄Action
 * 負責取得表單數據、調用業務邏輯、返回轉向信息
 * @author liuzhengquan
 *
 */
public class LoginAction extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  LoginActionForm laf=(LoginActionForm)form;
  String userName=laf.getUserName();
  String password=laf.getPassword();
  UserManager userManager=new UserManager();
  try {
   userManager.login(userName, password);
   
   return mapping.findForward("success");
  } catch (UserNoFoundException e) {
   e.printStackTrace();
   request.setAttribute("msg", "用戶不能找到,用戶名稱=【"+userName+"】");
  }catch (PasswordErrorException e) {
   e.printStackTrace();
   request.setAttribute("msg", "密碼錯誤");
  }
  return mapping.findForward("error");
 }
}

 


5、配置struts-config.xml文件。在struts(一)裡面我們通過if…else判斷截取的URL,根據相應的URL,調用相應的Action。在struts框架裡面已經對此做了簡單的封裝,我們只需要將action和actionForm配置在struts-config.xml文件裡面。通過映射他們的關系,來實現匹配工作,替代了原來的if…else語句。

 

 

[java] 
<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE struts-config PUBLIC 
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" 
          "http://struts.apache.org/dtds/struts-config_1_2.dtd"> 
<struts-config> 
    <form-beans> 
        <form-bean name="LoginForm" type="com.xxjstgb.struts.LoginActionForm"></form-bean> 
    </form-beans> 
    <action-mappings> 
        <action path="/login"  
                type="com.xxjstgb.struts.LoginAction" 
                name="LoginForm" 
                scope="request"> 
            <forward name="success" path="/login_success.jsp"></forward> 
            <forward name="error" path="/login_failed.jsp"></forward> 
        </action> 
    </action-mappings> 
</struts-config> 

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
 <form-beans>
  <form-bean name="LoginForm" type="com.xxjstgb.struts.LoginActionForm"></form-bean>
 </form-beans>
 <action-mappings>
  <action path="/login"
    type="com.xxjstgb.struts.LoginAction"
    name="LoginForm"
    scope="request">
   <forward name="success" path="/login_success.jsp"></forward>
   <forward name="error" path="/login_failed.jsp"></forward>
  </action>
 </action-mappings>
</struts-config>

 


6、訪問


[html]
<body> 
    <form action="login.do" method="post"> 
        用戶名:<input type="text" name="userName"><br> 
        密 碼:<input type="password" name="password"> <br> 
        <input type="submit" value="登錄"> 
    </form> 
</body> 

<body>
 <form action="login.do" method="post">
  用戶名:<input type="text" name="userName"><br>
  密 碼:<input type="password" name="password"> <br>
  <input type="submit" value="登錄">
 </form>
</body>

至此,通過struts框架完成了登錄實例。


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved