分享
 
 
 

Struts初体验之实施篇

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Struts初体验之实施篇

baitianhai baichenhong@hotmail.com

开发环境 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,所以把自己的经过给大家分享,欢迎大家批评指正

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有