Struts 1.3(第一例) - Login
本想跳过直接学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的更多相关文章
- 统计iis日志第一例的次数
统计iis日志第一例(日期)出现的次数 IIS日志文件格式: #Software: Microsoft Internet Information Services 7.5 #Version: 1.0 ...
- PythonQt第一例
pythonQt第一例源码如下,主要介绍了简单的使用方式,需要注意的是应用程序的debug版本和release版本必须使用同类型的PythonQt库不可交叉使用. 源码地址:http://files. ...
- struts快速入门第一篇 —— struts相关XML配置映射及讲解
我们回忆一下在学习JavaWeb过程中(Jsp + servlet编程)所感受到的Servlet的不足: 1 Servllet很多时,web.xml中的代码会很多.这样一来,维护起来就不方便,不利于团 ...
- php第一例
参考 例子 https://www.cnblogs.com/chinajins/p/5622342.html 配置多个网站 https://blog.csdn.net/win7system/artic ...
- Spring入门第一例
通过多天对基础语法的学习,早就向往一睹SPRING的芳容.今天按照ITEYE 唐的 教程,第一次运行Spring成功,步骤及注意事项如下: 一.基础环境 Jdk1.8, Eclipse4.71 .Sp ...
- Vue3学习第一例:Vue3架构入门
入门 Vue3的教程很少,官方网站实例不好整,另外由于Python的Django也掌握了,学习这个有些让人眼乱.Vue项目创建后,在public目录下面自动生成了一个index.htm,里面有个div ...
- esp32编程第一例 hollow word
#include<stdio.h>#include"freertos/FreeRtos.h"#include"freertos/task.h"#in ...
- Struts+Hibernate+Spring实现用户登录功能
通过登录案例实现三大框架之间的整合,登录功能是任何系统和软件必不可少的一个模块,然而通过这个模块来认识这些复杂的框架技术,理解数据流向和整个设计思路是相当容易的.只有在掌握了这些小模块的应用后,才能轻 ...
- PHP程序设计经典300例
不知道怎么转载,原文源自:http://bbs.php100.com/u-htm-uid-330857.html 来自:php100钟泽锋 第一例<?php $s_html="< ...
随机推荐
- Qt的quit(),exit()以及close()事件捕获
使用QT编辑界面,其中带来很大方便的一点就是Qt中自带丰富的.种类齐全的类及其功能函数,程序员可以在编辑程序的过程中简单地直接调用.关于窗口关闭的操作,在这里指出常用的三个槽,即quit() ...
- Python 如何跳出多重循环
Python 如何跳出多重循环 抛异常 return
- 【转】使用cocos2d-console工具转换脚本为字节码
从Cocos2D-X v2.1.4版本开始,增加了Cocos2D-console命令行工具,该工具的其中一个功能是:把.js文件转换为.jsc文件,该文件是字节码格式,可以提高代码的安全性. 使用这个 ...
- 基于 Qt的聊天工具
主要参考:http://blog.csdn.net/zouxy09/article/details/9140881
- [转]在Eclipse中Debug 为什么显示source not found
在Eclipse中Debug 为什么显示source not found http://zhidao.baidu.com/link?url=-jna2HB_k2FW72GPbT--5Qg2AWi3Ip ...
- memcache 线程深入理解分析 及 源码研究
http://blog.csdn.net/huithe/article/details/8006186
- 恢复Ext3下被删除的文件(转)
恢复Ext3下被删除的文件(转) 前言 下面是这个教程将教你如何在Ext3的文件系统中恢复被rm掉的文件. 删除文件 假设我们有一个文件名叫 ‘test.txt’ $ls -il test.txt 1 ...
- (转) Deep Learning in a Nutshell: Reinforcement Learning
Deep Learning in a Nutshell: Reinforcement Learning Share: Posted on September 8, 2016by Tim Dettm ...
- Unity3D研究院之使用Animation编辑器编辑动画
Unity提供了Animation编辑器,它可以为我们编辑物理动画.举个例子比如场景中有一个来回摇动的秋千,这个秋千在项目中完全只起到衬托作用,它不会与别的游戏对象有任何交互.如果这个秋千也用代码来 ...
- Linux 数据流重定向
1.三种数据流重定向1)标准输入(stdin):代码为0,使用0<或0<<,其中代码0可以省略2)标准输出(stdout):代码为1,使用1>或1>>,其中代码1可 ...