struts安装与示例

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  
* @version 1.0 
*/ 
response.setHeader("Pragma","No-cache"); 
response.setHeader("Cache-Control","no-cache"); 
response.setDateHeader("Expires", 0); 
%> 
 
 
Logon 
 
 
 
 
username: 
password: 
 
 
 


页面为用户登陆页面,应用到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

 
 
 
Struts Logon Application 
 
 
action 
org.apache.struts.action.ActionServlet 
 
config 
/WEB-INF/struts-config.xml 
 
 
debug 
2 
 
 
detail 
2 
 
2 
 
 
 
action 
*.do 
 
 
 
logon.jsp 
 
 
 
/tags/struts-bean 
/WEB-INF/struts-bean.tld 
 
 
/tags/struts-html 
/WEB-INF/struts-html.tld 
 
 
/tags/struts-logic 
/WEB-INF/struts-logic.tld 
 
 
/tags/struts-nested 
/WEB-INF/struts-nested.tld 
 
 
/tags/struts-tiles 
/WEB-INF/struts-tiles.tld 
 


2.5 struts-config.xml

 
 
 
 
 
 
 
 
 
 
 
 


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入门之用。

你可能感兴趣的:(J2EE,struts,import,string,action,servlet,html)