struts1 Demo
每次都会忘记一些东西,反复查找原因,其实struts1很简单,可是不去巩固也很容易忘记并且犯错误。这是一个最简单的登录Demo.
1.建立web工程,引入struts1.2包
2.建package:action和form。
2.1在form下建userform.java
package form;
import org.apache.struts.action.ActionForm;
public class UserForm extends ActionForm {
/**
* formBean
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
2.2在LoginAction.java:
如果账户名为”123“,则登录成功。
package action; 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; import form.UserForm; public class LoginAction extends Action { /*
* (non-Javadoc)
*
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.
* ActionMapping, org.apache.struts.action.ActionForm,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
System.out.println("LoginAction.execute()");
UserForm user = (UserForm) form;
System.out.println("name = " + user.getUsername());
if (user.getUsername().equals("123")) {
return mapping.findForward("success");
} else {
return mapping.findForward("error");
}
} }
3.配置文件:
3.1在WEB-INF下建立struts-config.xml
(这一步是关键步骤!学会了action的配置基本就掌握了struts1)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="userform" type="form.UserForm"/>
</form-beans>
<action-mappings>
<action
attribute="userform"
path="/login"
name="userform"
scope="request"
type="action.LoginAction"
input="/index.jsp"
>
<forward name="success" path="/WEB-INF/wel.jsp"/>
<forward name="error" path="/WEB-INF/error.jsp"/>
</action>
</action-mappings>
</struts-config>
3.2配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- servlet class ?? -->
<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>
</web-app>
3.3新建jsp页面文件
index.jsp (作为input source)
<form action="login.do" method="post">
name: <input type="text" name="username">
<input type="submit" value="submit">
</form>
跳转页面wel.jsp 和error.jsp
<body>
This is wel JSP page. <br>
hello <%=request.getParameter("username") %>
</body>
struts1 Demo的更多相关文章
- struts1的一些基本用法和操作
入职两周了,项目是用struts1+ibatis框架搭建的,数据库是oracle,其他还行,关键是struts1之前没用用过,所以只好在网上狂查文档,最后大致整理了一些struts1的基本使用方法. ...
- jsp\struts1.2\struts2 中文件上传(转)
jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...
- struts2 Demo
参考资料 :http://www.cnblogs.com/yangy608/archive/2010/11/08/1871962.htmlhttp://www.yiibai.com/struts2/s ...
- struts1老古董配置
<!--Struts1 struts-config.xml Demo --><?xml version="1.0" encoding="UTF-8&qu ...
- Struts1基础、使用Struts实现登录、使用Struts HTML标签简化开发
Struts 1基础 为什么重拾Struts 1 曾经是最主流的MVC框架 市场份额依然很大 很多遗留系统中依旧使用 维护和升级都需要熟悉Struts 1 与Struts 2相比 编码.配置繁琐 侵入 ...
- Struts1标签
Struts1 标签库 说明 Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML 标签 : 用来创建能够和Struts 框架和其他相应的HT ...
- 分享知识-快乐自己:初始 Struts2 (基本概念)及 搭建第一个Demo
1):struts2 的基本概念: 1-1):Struts2 是什么? 1.Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2 ...
- 通过一个demo了解Redux
TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...
- 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo
有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...
随机推荐
- PDF 补丁丁 0.4.2.1023 测试版使用手册发布
由于 PDF 补丁丁新版本增加的功能颇多,界面也重新设计过,因此,使用手册需要作比较大幅度的改动才能与软件相匹配. 目前,使用手册已经撰写了一部分,但新增功能的介绍和新界面的截图还没完成,有2/3的内 ...
- Octopus系列之UploadValues异步上载
不多说了直接上代码 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "tex ...
- Windows下搭建Git开发环境
Windows下搭建Git开发环境主要有以下三种方法: 1,VS,vs2013和vs2015中已经集成了git插件了 2,msysGit+TortoiseGit 3,msysGit+SourceTre ...
- You Only Live Once
从做 PreAngel 以来,每年我都会抽空去美国一两次,主要是在硅谷(湾区)一带见见当地的朋友,他们主要有 VC.创业者.斯坦福和伯克利的学生创业组织负责人.无线科技领域的各种组织机构负责人等,我一 ...
- 1900. Brainwashing Device
http://acm.timus.ru/problem.aspx?space=1&num=1900 题目大意: 有N个车站,相邻车站之间形成一个段,这样就有N-1个段,每个段最多可以放一个洗脑 ...
- oracle生成行方法
数据库记录是行的集合 set of row, 那么如何如何生成集合呢? oracle中常用的是 递归查询(with ... union all ...) 以及 connect by(树形查询) htt ...
- 百度定位并获取县区天气-XML+fragment+sqlite
此工程较BaiduLocationXMLFragment相比:1.加入数据库部分,将获取到的地址 天气存入数据库中,离线状态显示数据库最后一条记录 sqlite: DatabaseHelper.ja ...
- jQueryMobile控件之ListView
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ubuntu操作系统安装使用教程 (转)
随着微软的步步紧逼,包括早先的Windows黑屏计划.实施,逮捕番茄花园作者并判刑,种种迹象表明,中国用户免费使用盗版Windows的日子将不会太长久了,那么这个世界上有没有即免费又易用的操作系统呢? ...
- understanding Nhibernate Hilo
http://stackoverflow.com/questions/2738671/please-explain-nhibernate-hilo http://stackoverflow.com/q ...