下面这个Struts 1.1 安装、示例篇教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!
【 tulaoshi.com - 编程语言 】
在前一篇文章中,我们具体的介绍了Struts 1.1 的工作原理,接着将会介绍它的安装,以及一个具体的应用。
1. Struts 1.1 安装
安装Struts 1.1 比较简单。假设我们使用的是Tomcat 5.0,并且安装在D: omcat下。首先,请到http://jakarta.apache.org/struts/ 下载Struts 1.1,解压缩后,可以看到jakarta-struts-1.1目录,在这个目录下面,你可以看到这样一个目录:webapps.该目录下面有多个*.war,我们copy struts-blank.war 文件到D: omcatwebapps下,restart Tomcat,在Tomcat 的webapps下就会生成一个struts-blank空白Struts开发环境。另外,在你解压缩的目录下的webapps中 struts-example.war 是一个很好的struts学习教程,假如你想深入学习请访问http://jakarta.apache.org/struts 或者 下载 《Struts in Action》 E 文版。
2. 一个简单的例子
2.1 Logon.jsp
%@ taglib uri="/WEB-INF/struts-Html.tld" prefix="html" %
%@ page contentType="text/html; charset=gb2312" %
%
/**
* This page shows Logon interface.
* Title: LogoN SYSTEM
* Description: Logon Page
* Copyright: Copyright (c) 2004
* Company: www.ewe.com.cn
* @author bluesunny hyjun2001@163.com
* @version 1.0
*/
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("EXPires", 0);
%
html
head
titleLogon/title
/head
body
html:errors/
html:form action="/Logon" focus="username"
username:html:text property="username" size="25"/
passWord:html:password property="password" size="25"/
html:submit property=submit/
/html:form
/body
/html
页面为用户登陆页面,应用到Struts的html标签。
2.2 LogonForm.Java
package com.Logon.app;
import javax.servlet.http.*;
import org.apache.struts.action.*;
/**
* This page shows LogonForm.java
* Title: LogoN SYSTEM
* Description: LogonForm Page
* Copyright: Copyright (c) 2004
* Company: www.ewe.com.cn
* @author bluesunny
* @version 1.0
*/
public class LogonForm extends ActionForm{
private String username;
private String password;
public LoginForm() {
username = null;
password = null;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername() {
return this.username;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return this.password;
}
public void reset(ActionMapping mapping, HttpServletRequest request)
{
username = null;
password = null;
}
}
这个页面为ActionForm bean ,主要是用来封装客户端的用户名与密码。
2.3 LogonAction.java
package com.Logon.app;
import java.io.*;
import java.sql.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
/**
* This page shows LogonAction.java
* Title: LogoN SYSTEM
* Description: LogonAction Page
* Copyright: Copyright (c) 2004
* Company: www.ewe.com.cn
* @author bluesunny
* @version 1.0
*/
public class LogonAction extends Action{
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)throws Exception{
LogonForm form = (LogonForm)actionForm;
ActionErrors errors = new ActionErrors();
String username = form.getUsername();
String password = form.getPassword();
If(username.equals(bluesunny) && password.equals(bluesunny)){
return actionMapping.findForward("sUCcess");
}else{
errors.add("login",new ActionError("Logon.failed"));
saveErrors(request,errors);
return actionMapping.findForward("failed");
}
}
}
取得ActionForm中的数据,进行处理,处理结果返回到Jsp页面。
2.4 web.XML
?xml version="1.0" encoding="ISO-8859-1"?
!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"
web-app
display-nameStruts Logon Application/display-name
!-- Standard Action Servlet Configuration (with debugging) --
servlet
servlet-nameaction/servlet-name
servlet-classorg.apache.struts.action.ActionServlet/servlet-class
init-param
param-nameconfig/param-name
param-value/WEB-INF/struts-config.xml/param-value
/init-param
init-param
param-namedebug/param-name
param-value2/param-value
/init-param
init-param
param-namedetail/param-name
param-value2/param-value
/init-param
load-on-startup2/load-on-startup
/servlet
!-- Standard Action Servlet Mapping --
servlet-mapping
servlet-nameaction/servlet-name
url-pattern*.do/url-pattern
/servlet-mapping
!-- The Welcome File List --
welcome-file-list
welcome-fileLogon.jsp/welcome-file
/welcome-file-list
!-- Struts Tag Library Descriptors --
taglib
taglib-uri/tags/struts-bean/taglib-uri
taglib-location/WEB-INF/struts-bean.tld/taglib-location
/taglib
taglib
taglib-uri/tags/struts-html/taglib-uri
taglib-location/WEB-INF/struts-html.tld/taglib-location
/taglib
taglib
taglib-uri/tags/struts-logic/taglib-uri
taglib-location/WEB-INF/struts-logic.tld/taglib-location
/taglib
taglib
taglib-uri/tags/struts-nested/taglib-uri
taglib-location/WEB-INF/struts-nested.tld/taglib-location
/taglib
taglib
taglib-uri/tags/struts-tiles/taglib-uri
taglib-location/WEB-INF/struts-tiles.tld/taglib-location
/taglib
/web-app
2.5 struts-config.xml
?xml version="1.0" encoding="ISO-8859-1" ?
!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"
struts-config
form-beans
form-bean name="LogonForm" type=" com.Logon.app.LoginForm"/
/form-beans
action-mappings
action
path="/Logon" type="com.Logon.app.LogonAction" name="LogonForm" scope="request" input="Logon.jsp"
forward name="success" path="/success.jsp"/
forward name="failure" path="/Logon.jsp"/
/action
message-resources parameter="resources.application"/
/struts-config
2.6 application.properties
Logon.failed = Logon failed!Please Logon in again!
2.7 deploy Logon Application
创建一个success.jsp,输入:"successflly!".
编译源文件,并且把该应用程序部署在TOMCAT里,假设部署在app目录下面。我们可以启动浏览器访问:http://localhost:8080/app/.
2.8 目录结构
/WEB-INF :
/ WEB-INF/lib : Struts.jar
/ WEB-INF/classes :
/ WEB-INF/classes/com/Logon/app : *.class
/ WEB-INF/classes/resources : *.properties
3. 总结
本文具体讲解了安装方法,并且举了一个登陆例子作为Struts框架应用。另外,本文对Struts的目录结构也有说明,这大大减轻了初学者学习负担,应该可以作为Struts入门之用。
来源:http://www.tulaoshi.com/n/20160219/1601837.html
看过《Struts 1.1 安装、示例篇》的人还看了以下文章 更多>>