本想跳过直接学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. 3des加解密算法

    编号:1003时间:2016年4月1日09:51:11功能:openssl_3des加解密算法http://blog.csdn.net/alonesword/article/details/17385 ...

  2. 扩展KVM镜像的虚拟磁盘大小

    当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作. 整个流程可以分为三个阶段: 1.扩展KVM镜像磁盘文件大小到30G ...

  3. 总结 group by 的用法

    今天用实例总结一下group by的用法. 归纳一下:group by:ALL ,Cube,RollUP,Compute,Compute by 创建数据脚本 Create Table SalesInf ...

  4. python---解决“Unable to find vcvarsall.bat”错误

    安装某些module时发生常见的 Unable to find vcvarsall.bat 错误 在eddsn找到了“Unable to find vcvarsall.bat” error when ...

  5. URAL 1205 By the Underground or by Foot?(SPFA)

    By the Underground or by Foot? Time limit: 1.0 secondMemory limit: 64 MB Imagine yourself in a big c ...

  6. 神奇的输入 while(cin>>....)如何在遇见换行之后进入下一层循环读入

    cin>>m>>n; ;i<=m;i++) { ; char ch=' '; ) //在遇到换行之后进入下一层循环读入. { x++; cin>>c[x]; ...

  7. hdu5441(2015长春赛区网络赛1005)类最小生成树、并查集

    题意:有一张无向图,一些点之间有有权边,某条路径的值等于路径上所有边的边权的最大值,而某个点对的值为这两点间所有路径的值的最小值,给出多个询问,每个询问有一个值,询问有多少点对满足其值小于等于询问值. ...

  8. 自我提升mysql

    1.某字段更新 自增 1 update table set a=a+1 2.修改某一字段的数据类型 alter table "tablename" modify  "co ...

  9. Unity学习资源

    NGUI文档及视频: http://www.tasharen.com/forum/index.php?topic=6754 动态更新的解决方案: http://game.ceeger.com/foru ...

  10. dede模板完全控制攻略

    1.使用php代码 @me代表了当前字段的值 {dede:tagname runphp='yes'} @me = "123456";//如果使用了自定义函数 则@me得到的是函数返 ...