Struts 1 学习笔记-1(简单登录模块的实现,Struts初步了解)

本文介绍如何使用Struts 1.2.9搭建一个简单的登录系统。从环境配置到各组件实现进行了详细说明,包括struts-config.xml和web.xml配置、JSP页面设计、ActionForm与Action类编写。

建立一个简单的登录系统:

1.本人下载的是Stuts 1.2.9 ,解压后将lib下的jar包拷贝到web-inf / lib下,包括以下几个:

2.在web-inf下建立struts-config.xml文件,代码如下:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>

<struts-config>
        
</struts-config>

3.修改web.xml文件:

<?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"
>
    
  
<welcome-file-list>
    
<welcome-file>index.jsp</welcome-file>
  
</welcome-file-list>
  
  
<servlet>
    
<servlet-name>action</servlet-name>
    
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    
<init-param>
      
<param-name>config</param-name>
      
<param-value>/WEB-INF/struts-config.xml</param-value>
    
</init-param>
    
<init-param>
      
<param-name>debug</param-name>
      
<param-value>2</param-value>
    
</init-param>
    
<init-param>
      
<param-name>detail</param-name>
      
<param-value>2</param-value>
    
</init-param>
    
<load-on-startup>2</load-on-startup>
  
</servlet>


  
<!-- Standard Action Servlet Mapping -->
  
<servlet-mapping>
    
<servlet-name>action</servlet-name>
    
<url-pattern>*.do</url-pattern>
  
</servlet-mapping>

</web-app>

4.创建用户登录界面login.jsp(action=login.do),login_success.jsp,login_error.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
   
  
</head>
  
  
<body>
    
<H1>登录页面</H1>
    
    
<HR>
    
<form action="login.do">
          用户名称:
<input type="text" name="username"><br>
          用户密码:
<input type="password" name="password"><br>
      
      
<HR>
      
                  
<input type="submit" name="btn_login" value="登录"><br>  
    
</form>
      
  
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
    
<H1>登录成功</H1>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
    
<H1>登录失败</H1>
</body>
</html>

5.创建LoginActionForm.java集成ActionForm,注意该类的成员属性名称应与form表单中的一致,并且要生成相应的getter and setter,封装好:

package com.ycringfinger.struts;

import org.apache.struts.action.ActionForm;

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

    
}

 

6.创建LoginAction.java继承自Action,并要重写其execute方法,在这里我让该方法接收表单数据并判断用户名及密码,如成功则跳转到login_success.jsp,若抛出Exception,则跳转至login_error.jsp:

package com.ycringfinger.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;

public class LoginAction extends Action {

    
    
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();
        
try {
            UserManager.getInstance().validate(username, password);
            
return mapping.findForward("success");
        }
 catch (UserNotFoundException e) {
            e.printStackTrace();
        }
 catch (PasswordNotCorrectException e) {
            e.printStackTrace();
        }

        
return mapping.findForward("error");
    }

    
}
    

7.配置stuts-config.xml,注意<action path="/login" type="com.ycringfinger.struts.LoginAction" name="loginActionForm" scope="request">中/login 对应了form中的action=login.do,name="loginActionForm"则对应了form-bean中的loginActionForm,至于scope你则可以根据实际情况选择。

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>

<struts-config>
    
    
<form-beans>
        
<form-bean name="loginActionForm" type="com.ycringfinger.struts.LoginActionForm"></form-bean>
    
</form-beans>
    
    
<action-mappings>
        
<action path="/login" type="com.ycringfinger.struts.LoginAction" name="loginActionForm" scope="request">
            
<forward name="success" path="/login_success.jsp"></forward>
            
<forward name="error" path="/login_error.jsp"></forward>
        
</action>
    
</action-mappings>
    
</struts-config>

 

8.大功告成,测试。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值