Struts初体验之实施篇
baitianhai [email protected]
开发环境 window2000+jbuilder7+tomcat4.1.12
首先,从http://jakarta.apache.org下载jakarta-struts-1.0.2.zip,解开后有两个主要目录lib和webapps,在lib目录中是使用struts创建应用程序所必需的文件,在webapps目录下有几个后缀是war的文件,熟悉java web开发的人都比较清楚,将他们copy到tomcat/ webapps/目录下,然后再重新启动tomcat,tomcat会将这些web工程部署好的,其中Struts-documentation.war是文档,Struts-example.war是一个不错的例子,我主要就是参考这个例子来做的。然后在jbuilder安装目录/lib/下建立 struts目录,将上面的lib目录下的东西copy进去,然后启动jbuilder7,运行菜单tools->configure->libraries->new->name 输入struts,然后选择刚才copy过去的struts.jar文件,然后OK。
其次,新建立一个工程,引用struts,服务器选择tomcat4,然后建立一个Jsp1.jsp文件,然后把lib/目录下的所有.tld文件copy到你建立的工程目录下的/defaultroot/WEB-INF/目录下面,在/defaultroot/WEB-INF/建立action.xml,内容如下:
<action-mappings>
<forward name="logon" path="/logon.jsp"/>
<action path="/logon"
actionClass="example.LogonAction"
formAttribute="logonForm"
formClass="example.LogonForm"
inputForm="/logon.jsp">
<forward name="success" path="/Jsp1.jsp"/>
</action>
</action-mappings>
struts-config.xml文件,如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<form-beans>
<form-bean name="logonForm"type="example.LogonForm"/>
</form-beans>
<global-forwards>
<forward name="logon" path="/logon.jsp"/>
</global-forwards>
<action-mappings>
<action path="/logon"
type="example.LogonAction"
name="logonForm"
scope="request"
input="/logon.jsp">
<forward name="success" path="/Jsp1.jsp"/>
</action>
<action path="/admin/addFormBean"
type="org.apache.struts.actions.AddFormBeanAction"/>
<action path="/admin/addForward"
type="org.apache.struts.actions.AddForwardAction"/>
<action path="/admin/addMapping"
type="org.apache.struts.actions.AddMappingAction"/>
<action path="/admin/reload"
type="org.apache.struts.actions.ReloadAction"/>
<action path="/admin/removeFormBean"
type="org.apache.struts.actions.RemoveFormBeanAction"/>
<action path="/admin/removeForward"
type="org.apache.struts.actions.RemoveForwardAction"/>
<action path="/admin/removeMapping"
type="org.apache.struts.actions.RemoveMappingAction"/>
</action-mappings>
</struts-config>
然后建立logon.jsp,内容如下:
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html locale="true">
<head>
<title><bean:message key="logon.title"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:errors/>
<html:form action="/logon" focus="username">
<table border="0" width="100%">
<tr>
<th align="right">
<bean:message key="prompt.username"/>
</th>
<td align="left">
<html:text property="username" size="16" maxlength="16"/>
</td>
</tr>
<tr>
<th align="right">
<bean:message key="prompt.password"/>
</th>
<td align="left">
<html:password property="password" size="16" maxlength="16" redisplay="false"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit property="submit" value="Submit"/>
</td>
<td align="left">
<html:reset/>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
然后建立一个class LogonForm.java,继承自ActionForm,打包在example内容如下
package example;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LogonForm extends ActionForm {
private String password = null;
private String username = null;
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.password = null;
this.username = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((username == null) || (username.length() < 1))
errors.add("username", new ActionError("error.username.required"));
if ((password == null) || (password.length() < 1))
errors.add("password", new ActionError("error.password.required"));
return errors;
}
}
然后建立一个class LogonAction.java,继承自Action,打包在example内容如下
package example;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Locale;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;
public class LogonAction extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Locale locale = getLocale(request);
//MessageResources messages = getResources();
ActionErrors errors = new ActionErrors();
String username = ((LogonForm) form).getUsername();
String password = ((LogonForm) form).getPassword();
// Report any errors we have discovered back to the original form
if (!errors.empty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
// Save our logged-in user in the session
HttpSession session = request.getSession();
session.setAttribute("user", "ok");
if (servlet.getDebug() >= 1)
servlet.log("LogonAction: User :" +
"' logged on in session " + session.getId());
// Remove the obsolete form bean
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}
// Forward control to the specified success URI
return (mapping.findForward("success"));
}
}
然后修改/defaultroot/WEB-INF/web.xml文件为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>application</param-name>
<param-value>example.ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>debugjsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>jspCompilerPlugin</param-name>
<param-value>com.borland.jbuilder.webserverglue.tomcat.jsp.JasperSunJavaCompiler</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>debugjsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>logon.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/app.tld</taglib-uri>
<taglib-location>/WEB-INF/app.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>
最后还有一个重要的问题,就是一开始我们说过Struts-example.war,安装后把\struts-example\WEB-INF\src\org\apache\struts\webapp\ApplicationResources.properties放到/defaultroot/WEB-INF/classes/example/目录下,然后就可以运行了,当你运行jbuilder后,在IE上键入http://localhost:8080/就会看到效果了,具体的解释请参见下一 解释篇
刚开始研究struts,所以把自己的经过给大家分享,欢迎大家批评指正
本文地址:http://com.8s8s.com/it/it18076.htm