电脑技术学习

Struts初体验之实施篇

dn001
内容: 开发环境 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,内容如下:






actionClass="example.LogonAction"

formAttribute="logonForm"

formClass="example.LogonForm"

inputForm="/logon.jsp">







struts-config.xml文件,如下:

<?xml version="1.0" encoding="ISO-8859-1" ?>

struts-config PUBLIC

"//Apache Software Foundation//DTD Struts Configuration 1.0//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">



<form-beans>

<form-bean name="logonForm"type="example.LogonForm"/>

</form-beans>










type="example.LogonAction"

name="logonForm"

scope="request"

input="/logon.jsp">






type="org.apache.struts.actions.AddFormBeanAction"/>


type="org.apache.struts.actions.AddForwardAction"/>


type="org.apache.struts.actions.AddMappingAction"/>


type="org.apache.struts.actions.ReloadAction"/>


type="org.apache.struts.actions.RemoveFormBeanAction"/>


type="org.apache.struts.actions.RemoveForwardAction"/>


type="org.apache.struts.actions.RemoveMappingAction"/>




然后建立logon.jsp,内容如下:

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<head>
<bean:message key="logon.title"/>

</head>
<body bgcolor="white">


<table border="0" width="100%">
<tr>
<th align="right">

</th>
<td align="left">

</td>
</tr>
<tr>
<th align="right">

</th>
<td align="left">

</td>
</tr>
<tr>
<td align="right">

</td>
<td align="left">

</td>
</tr>
</table>

</body>

然后建立一个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"?>
web-app PUBLIC "//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">


action
org.apache.struts.action.ActionServlet

debug
2


config
/WEB-INF/struts-config.xml


application
example.ApplicationResources


detail
2


validate
true

2


debugjsp
org.apache.jasper.servlet.JspServlet

jspCompilerPlugin
com.borland.jbuilder.webserverglue.tomcat.jsp.JasperSunJavaCompiler

3


action
*.do


debugjsp
*.jsp


logon.jsp


/WEB-INF/app.tld
/WEB-INF/app.tld


/WEB-INF/struts-bean.tld
/WEB-INF/struts-bean.tld


/WEB-INF/struts-html.tld
/WEB-INF/struts-html.tld


/WEB-INF/struts-logic.tld
/WEB-INF/struts-logic.tld

最后还有一个重要的问题,就是一开始我们说过Struts-example.war,安装后把struts-exampleWEB-INFsrcorgapachestrutswebappApplicationResources.properties放到/defaultroot/WEB-INF/classes/example/目录下,然后就可以运行了,当你运行jbuilder后,在IE上键入http://localhost:8080/就会看到效果了,具体的解释请参见下一 解释篇

刚开始研究struts,所以把自己的经过给大家分享,欢迎大家批评指正

bill-转自:csdn Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 开发环?

标签: