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="< ...
随机推荐
- c++ 走向高级之日积月累
1.enum:http://en.cppreference.com/w/cpp/language/enum 2.weak_pr:http://en.cppreference.com/w/cpp/mem ...
- lanuchy快捷操作
down arrow: display history shift+delete: remove the item from the distory
- UVa 11246 - K-Multiple Free set
题意大意: 一个{1..n}的集合,求一个子集合,使得元素个数最多,并且不存在有两个元素x * k = y,求出最多的元素个数是多少. 分析: 先要删除k倍的,删除为{k, 2k, 3k, 4k, 5 ...
- Apache CXF 101 Win Eclipse开发环境搭建
前言 博客草稿中“SOA生态系统初探”一文一直没有进展,感觉要将SOA.Web Service(WS).REST等概念阐述清楚还需要一些酝酿. 顶天须得立地,这里记录一些“下里巴人”的实践,主要考察A ...
- sencha 环境配置
sencha generate app MyApp MyApp 首先需要注意的是安装 sencha 并执行相关命令. 搞了老半天才知道 想要创建空项目首先 必须的在sdk 目录下才可以.好像据说第二次 ...
- 274. H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...
- 67. Add Binary
public class Solution { public String addBinary(String a, String b) { char[] aa=a.toCharArray(); cha ...
- Jquery判断滚动条是否到达窗口顶部和底部
<script type="text/javascript"> $(document).ready(function(){ alert($(window).he ...
- Oracle语句
分页查询: select rn,last_name,salary from( select rownum rn,last_name,salary from( select last_name,sala ...
- ubuntu 14.04 apache maven 安装
下载maven http://maven.apache.org/download.cgi 解压 tar -xzvf apache-maven-3.0.5-bin.tar.gz -C /usr/loc ...