具体如下:

<span style="font-size:18px;"><?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></span>

首先这个配置文件中最主要的就是做了两件的事情,一个是配置ActionServlet,一个是初始化struts-config.xml配置文件参数。

3、配置完了web.xml文件,之后我们就要开始进入项目代码阶段了。

登录页面:

<%@ 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>
<form action="login.do" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"></br>
 <input type="submit" value="登录">
</form>
</body>
</html>

切记那个action后面的路径一定要是.do开头的,因为我们在web.xml中配置的是*.do。这里依旧不介绍为什么随后博客会深入分析。

4、建立两个异常类,一个是用户名未找到、一个是密码错误:

①用户名未找到

public class UserNotFoundException extends RuntimeException { 
public UserNotFoundException() {
 // TODO Auto-generated constructor stub
}
public UserNotFoundException(String message) {
 super(message);
 // TODO Auto-generated constructor stub
}
public UserNotFoundException(Throwable cause) {
 super(cause);
 // TODO Auto-generated constructor stub
}
public UserNotFoundException(String message, Throwable cause) {
 super(message, cause);
 // TODO Auto-generated constructor stub
}
}

②密码错误

public class PasswordErrorException extends RuntimeException { 
public PasswordErrorException() {
 // TODO Auto-generated constructor stub
}
public PasswordErrorException(String message) {
 super(message);
 // TODO Auto-generated constructor stub
}
public PasswordErrorException(Throwable cause) {
 super(cause);
 // TODO Auto-generated constructor stub
}
public PasswordErrorException(String message, Throwable cause) {
 super(message, cause);
 // TODO Auto-generated constructor stub
}
}

5、业务处理类代码:

public class UserManager { 
public void login(String username, String password) {
 if (!"admin".equals(username)) {
  throw new UserNotFoundException();
}  
 if (!"admin".equals(password)) {
  throw new PasswordErrorException();
}  
}
}

6、建立LoginActionForm类,这个类继承ActionForm类,简单说一下这个类,这个类主要是负责收集表单数据的,在这里一定要注意表单的属性必须和actionForm中的get和set方法的属性一致。这里依旧不深入解释,随后博客都会涉及到。

import org.apache.struts.action.ActionForm; 
/**
* 登录ActionForm,负责表单收集数据
* 表单的属性必须和ActionForm中的get和set的属性一致
* @author Administrator
*
*/
@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;
}  
}

7、LoginAction类的建立,这个是负责取得表单数据、调用业务逻辑以及返回转向信息。

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 Administrator
*
*/
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(UserNotFoundException e){
  e.printStackTrace();
  request.setAttribute("msg", "用户名不能找到,用户名称=["+username+"]");
}catch(PasswordErrorException e){
  e.printStackTrace();
  request.setAttribute("msg", "密码错误");
}
 return mapping.findForward("error");
}
}

8、既然有转向,那么我们还要建立两个页面,一个是登录成功页面,一个登录失败页面。

①登录成功页面

<%@ 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>
${loginForm.username },登录成功
</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>
<%--
<%=request.getAttribute("msg") %>
--%>
${msg }
</body>
</html>

9、最后要进行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>
  <form-beans>
    <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>
  </form-beans>
  <action-mappings>
    <action path="/login"  
        type="com.bjpowernode.struts.LoginAction"
        name="loginForm"    
        scope="request"    
        \>
      <forward name="success" path="/login_success.jsp" />
      <forward name="error" path="/login_error.jsp"/>    
    </action>
  </action-mappings>
</struts-config>

一个struts1框架的小案例的更多相关文章

  1. 一个DRF框架的小案例

    第一步:安装DRF DRF需要以下依赖: Python (2.7, 3.2, 3.3, 3.4, 3.5, 3.6) Django (1.10, 1.11, 2.0) DRF是以Django扩展应用的 ...

  2. JavaWeb_(Struts2框架)Ognl小案例查询帖子

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  3. 一个简单的Maven小案例

    Maven是一个很好的软件项目管理工具,有了Maven我们不用再费劲的去官网上下载Jar包. Maven的官网地址:http://maven.apache.org/download.cgi 要建立一个 ...

  4. SSM框架CRUD小案例

    1.数据库准备 部门tbl_dept 员工tbl_emp 建立员工和部门的外键 2.在IDEA创建SSM项目环境 2.1配置Web模块 最上面的图是错误示范,注意!!! 在Tomcat配置了项目路径, ...

  5. angular前端框架简单小案例

    一.angular表达式 <head> <meta charset="UTF-8"> <title>Title</title> &l ...

  6. 一个用户管理的ci框架的小demo--转载

    一个ci框架的小demo 最近在学习ci框架,作为一个初学者,在啃完一遍官方文档并也跟着官方文档的例程(新闻发布系统)做了一遍,决定在将之前练习PHP与MySQL数据库的用户管理系统再用ci框架实现一 ...

  7. 《java入门第一季》之类小案例(模拟用户登录)

    首先是做一个用户登录的小案例.在此基础上加入其它逻辑. import java.util.Scanner; /* * 模拟登录,给三次机会,并提示还有几次.如果登录成功,就可以玩猜数字小游戏了. * ...

  8. Ajax传递json数据简介和一个需要注意的小问题

    Ajax传递json数据 Ajax操作与json数据格式在实际中的运用十分广泛,本文为大家介绍一个两者相结合的小案例: 项目结构 我们新建一个Django项目,在里面创建一个名为app01的应用: p ...

  9. Vue小案例(一)

    案例需求: 创建一个品牌展示表格,表头有编号(id),品牌名称(name),创建时间(time)和操作,需要实现的功能是对数据的增删操作,和时间的格式化. 思路分析:在开发之前需要想清楚要用到Vue中 ...

  10. react框架实现点击事件计数小案例

    下面将以一个小案例来讲解react的框架的一般应用,重点内容在代码段都有详细的解释,希望对大家有帮助 代码块: 代码块: import React from 'react'; import React ...

随机推荐

  1. Path类,文件操作的路径用法

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  2. Gin加载history模式下打包后的Vue文件,刷新找不到页面404

    import ( "io/ioutil" "github.com/gin-contrib/static" "github.com/gin-gonic/ ...

  3. oracle内存管理

    关于内存管理 必须管理的内存结构是系统全局区(SGA)和实例程序全局区(instance PGA).Oracle 数据库支持各种内存管理方法,这些方法由初始化参数设置选择. 自动内存管理 Oracle ...

  4. sync.WaitGroup

    WaitGropu使用注意 作groutine参数时传指针 type WaitGroup struct { noCopy noCopy // 64-bit value: high 32 bits ar ...

  5. 学习dash篇-layout页面布局

    Dash介绍 Dash官网教程地址:https://dash.plotly.com/introduction 数据分析工作的结果,通常是数据表格.图表,分析报告.这些东西office的三件套基本都能满 ...

  6. Linux 三剑客常用命令

    shell三剑客===================================================grep===================================== ...

  7. 启动appium服务时报错,服务不通:Original error: Could not find 'apksigner.jar'

    启动时报错,服务不通:Original error: Could not find 'apksigner.jar' 是因为少了个文件,添加个文件就好了,可以参考下面的帖子. 可以参考这个帖子:http ...

  8. 【python】python3.7与3.9共存,两个3版本同时存在(平时用vscode敲代码)pip复制

    1.按照安装python及环境配置 - 人间寒梅 - 博客园 (cnblogs.com),将3.9装好. 2.在官网下载3.7的对应文件 3.下载后运行,并自定义下载且选中添加到path.,自己为py ...

  9. openlayers-1 下载及安装使用

    javascript - Import from in Openlayers - Geographic Information Systems Stack Exchange 在浏览器中运行开放层示例 ...

  10. 视觉里程计--视觉slam7.1/相机运动估计视觉算法

    视觉里程计 本篇文章记录了少许阅读<视觉slam14讲>的阅读整理,不是特别全面,只是为了本次项目中特定任务搜查资料,时间比较紧,文章并没有全面涵盖所有知识点.日后若时间有空闲,将回来补充 ...