本想跳过直接学Struts 2的,想想,还是先学Struts 1,万一到时去那个公司,人家用的是1,那还是要学,以及了解下1与2的区别在哪里。

上例子,很简单的一个网上login例子,再思考下Struts想干嘛。

Struts下载:

http://struts.apache.org/download.cgi#struts23161


先建三个jsp文件:

1)login.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">
Username:<input type="text" id="username" name="username" /><br/>
Password:<input type="password" id="password" name="password"/><br/>
<input type="submit" value="Login" />
</form>
</body>
</html>

2)main.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
Welcome:${ username }
</body>
</html>

3)error.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
Error
</body>
</html>

新建一个bean:UserForm,这个bean是Form Bean,对应的是login.jsp里头form中的控件name:

package com.my.forms;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {
public UserForm() {} private String username;
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;
} private String password;
}

新建 一个Action:LoginAction,这个action需要重写execute(...)方法,其中,参数form为bean form(Struts自动匹配)

package com.my.actions;

import javax.servlet.http.*;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; import com.my.forms.*; public class LoginAction extends Action {
public LoginAction() {
} @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 将Form强转为UserForm
UserForm userForm = (UserForm) form;
String username = userForm.getUsername();
String password = userForm.getPassword();
// 将用户名存入request表单域中
request.setAttribute("username", username);
if ("123".equals(password))
return mapping.findForward("main");
else
return mapping.findForward("error");
}
}

在/WEB-INF中新建一个xml文件:struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-Apache Software Foundation//DTD struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="userForm" type="com.my.forms.UserForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/login" name="userForm" scope="request" type="com.my.actions.LoginAction">
<forward name="main" path="/main.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>
</struts-config>

在web.xml中加入servlet的配置:

    <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>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

完成。


Struts 1.3(第一例) - Login的更多相关文章

  1. 统计iis日志第一例的次数

    统计iis日志第一例(日期)出现的次数 IIS日志文件格式: #Software: Microsoft Internet Information Services 7.5 #Version: 1.0 ...

  2. PythonQt第一例

    pythonQt第一例源码如下,主要介绍了简单的使用方式,需要注意的是应用程序的debug版本和release版本必须使用同类型的PythonQt库不可交叉使用. 源码地址:http://files. ...

  3. struts快速入门第一篇 —— struts相关XML配置映射及讲解

    我们回忆一下在学习JavaWeb过程中(Jsp + servlet编程)所感受到的Servlet的不足: 1 Servllet很多时,web.xml中的代码会很多.这样一来,维护起来就不方便,不利于团 ...

  4. php第一例

    参考 例子 https://www.cnblogs.com/chinajins/p/5622342.html 配置多个网站 https://blog.csdn.net/win7system/artic ...

  5. Spring入门第一例

    通过多天对基础语法的学习,早就向往一睹SPRING的芳容.今天按照ITEYE 唐的 教程,第一次运行Spring成功,步骤及注意事项如下: 一.基础环境 Jdk1.8, Eclipse4.71 .Sp ...

  6. Vue3学习第一例:Vue3架构入门

    入门 Vue3的教程很少,官方网站实例不好整,另外由于Python的Django也掌握了,学习这个有些让人眼乱.Vue项目创建后,在public目录下面自动生成了一个index.htm,里面有个div ...

  7. esp32编程第一例 hollow word

    #include<stdio.h>#include"freertos/FreeRtos.h"#include"freertos/task.h"#in ...

  8. Struts+Hibernate+Spring实现用户登录功能

    通过登录案例实现三大框架之间的整合,登录功能是任何系统和软件必不可少的一个模块,然而通过这个模块来认识这些复杂的框架技术,理解数据流向和整个设计思路是相当容易的.只有在掌握了这些小模块的应用后,才能轻 ...

  9. PHP程序设计经典300例

    不知道怎么转载,原文源自:http://bbs.php100.com/u-htm-uid-330857.html 来自:php100钟泽锋 第一例<?php $s_html="< ...

随机推荐

  1. Qt之QFileSystemWatcher

    简述 QFileSystemWatcher类用于提供监视文件和目录修改的接口. QFileSystemWatcher通过监控指定路径的列表,监视文件系统中文件和目录的变更. 调用addPath()函数 ...

  2. cuffdiff 和 edgeR 对差异表达基因的描述

    ASE又走到了关键的一步  要生成能决定是否有差异表达的table. 准备借鉴一下cuffdiff和edgeR 的结果 cuffdiff对差异表达基因的描述: 一共十四列: 第一列, test_id ...

  3. meta标签兼容性

    基本标签SEO 优化为移动设备添加 viewportWindows 8其他 禁止数字识自动别为电话号码不让android识别邮箱每 8 秒刷新一次页面移动端的头部标签和meta 基本标签 声明文档使用 ...

  4. Linux下解决“shutdown: command not found"问题

    今天关机时,使用shutdown来执行此操作,但出现了一个"bash:shutdown:command not found"错误提示.这让我很困惑,这个命令在系统中是肯定存在的,但 ...

  5. jsoup的elements类

    jsoup的Elements类 一.简介 该类是位于select包下,直接继承自Object,所有实现的接口有Cloneable, Iterable<Element>, Collectio ...

  6. 《C标准库》—之<assert.h>实现

    首先,贴出标准库中<assert.h>的实现源码: #undef assert #ifdef NDEBUG #define assert(test)((void)0) #else void ...

  7. 第四课,T语言运算符(版本5.0)

    TC综合开发工具里支持了丰富的运算符,这样也要求大家对运算符的知识必须了解清楚,否则出现错误还不知道问题所在下面就为大家说说运算符的优先级与各个运算符含义 注意: 优先级代表同一表达式中运算符的运算顺 ...

  8. JS初学之-选项卡(图片切换类)

    初学选项卡,主要问题卡在了索引值上面,花了较长的时间学习. 索引值其实很好理解,就是为每一个元素用JS的方法添加一个属性,即自定义属性. 在for循环里的函数里用i,会直接弹出这个数组的length, ...

  9. linux文件系统---10

    进入 Linux 根目录(即“/”, Linux 文件系统的入口, 也是处于最高一级的目录),运行“ls –l”命令,可以看到 Linux 系统包含以下目录. 1./bin 包含基本命令,如 ls.c ...

  10. google-http-java-client(android学习篇2源码)

    package com.google.api.services.samples.googleplus.cmdline.simple;   import com.google.api.client.ht ...