Struts 1.1 安装、示例篇
Struts 1.1 安装、示例篇
在前一篇文章中,我们详细的介绍了Struts 1.1 的工作原理,接着将会介绍它的安装,以及一个具体的应用。
1. Struts 1.1 安装
安装Struts 1.1 比较简单。假设我们使用的是Tomcat 5.0,并且安装在D:\tomcat下。首先,请到http://jakarta.apache.org/struts/ 下载Struts 1.1,解压缩后,可以看到jakarta-struts-1.1目录,在这个目录下面,你可以看到这样一个目录:webapps.该目录下面有多个*.war,我们copy struts-blank.war 文件到D:\tomcat\webapps下,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 <[email protected]>
* @version 1.0
*/
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
%>
<html>
<head>
<title>Logon</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-name>Struts Logon Application</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<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>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>logon.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.51dibs.com/lp07/la/a/l_a_9b60c43e98793486.html
在前一篇文章中,我们详细的介绍了Struts 1.1 的工作原理,接着将会介绍它的安装,以及一个具体的应用。
1. Struts 1.1 安装
安装Struts 1.1 比较简单。假设我们使用的是Tomcat 5.0,并且安装在D:\tomcat下。首先,请到http://jakarta.apache.org/struts/ 下载Struts 1.1,解压缩后,可以看到jakarta-struts-1.1目录,在这个目录下面,你可以看到这样一个目录:webapps.该目录下面有多个*.war,我们copy struts-blank.war 文件到D:\tomcat\webapps下,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 <[email protected]>
* @version 1.0
*/
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
%>
<html>
<head>
<title>Logon</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-name>Struts Logon Application</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<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>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>logon.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.51dibs.com/lp07/la/a/l_a_9b60c43e98793486.html
- 上篇文章:J2EE Frameworks介绍
- 下篇文章:Struts框架基础篇