第五篇:Struts小试牛刀篇

严重警告:如果你跳过了上篇,在本篇遇到了的任何难题,本人概不负责<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

开场白: 首先我希望你明白Struts是怎么工作的。在jsp页面上,当我们发生了提交动作(你就理解为当按钮按下的时候),页面上是数据(比如:文本框、复选框、单选框、下拉筐等中的内容)会自动写入到form中,当页面需要数据的时候也从form中取得。而action可以从form中取得相关的数据,并可以对这些数据进行处理,并把处理的结果也放到form中。

以前我们这样定义一个文本框:

<input type="text" name="str1">

现在利用了Struts的标签库,我们这样定义一个文本框

<html:text property="str1" />

这样定义的文本框,当页面上的submit按钮被按下的时候,这个文本框中的内容会自动的被放到from中,并且是放到form的一个str1属性中,为此,form会定义这个属性的get方法和set方法

用例子来说明问题:

工程的名字就叫:user

(多么的希望你知道把你的user放在什么地方,user里面有些什么东西)

这个例子的外观效果是这样的,首先第一个页面有两个文本框:分别输入你的姓(FirstName)和你的名(LastName),还有一个“提交”按纽,当按纽按下的时候,会调用另外的页面把名字姓和名整和起来显示出来。如果姓或者名有一个没有输入数据就“提交”的话,则转向另外的警告页面。不知道我说清楚了“需求”没有,下面让我们来看看效果吧。

你需要什么:请把下面的12的所有东西都放在WEB-INF下,让他们并肩站者、平起平坐,而且以后的程序中,我再不说了,请你自觉的把它们放在你的应用下的WEB-INF

1struts的标签库文件,这些东西如果你又找不到的话,请你还是给我email

struts-bean.tldstruts-config.xml.bakstruts-html.tldstruts-logic.tldstruts-nested.tldstruts-template.tldstruts-tiles.tld

2:两个xml配置文件

web.xml的内容如下,如果以后我不特别的说明,web.xml的内容就一下面的为准

<?xml version="1.0" encoding="gb2312"?>

<!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>

<!-- Action Servlet Configuration -->

<servlet>

<servlet-name>action</servlet-name>

<!-- Specify servlet class to use:

- Struts1.0.x: ActionComponentServlet

- Struts1.1: ActionServlet

- no Struts: TilesServlet

-->

<!-- <servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class> -->

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<!-- <servlet-class>org.apache.struts.tiles.TilesServlet</servlet-class> -->

<!-- Tiles Servlet parameter

Specify configuration file names. There can be several comma

separated file names

-->

<init-param>

<param-name>definitions-config</param-name>

<param-value>/WEB-INF/tiles-defs.xml,

/WEB-INF/tiles-s-defs.xml

</param-value>

</init-param>

<!-- Tiles Servlet parameter

Specify Tiles debug level.

O : no debug information

1 : debug information

2 : more debug information

-->

<init-param>

<param-name>definitions-debug</param-name>

<param-value>1</param-value>

</init-param>

<!-- Tiles Servlet parameter

Specify Digester debug level. This value is passed to Digester

O : no debug information

1 : debug information

2 : more debug information

-->

<init-param>

<param-name>definitions-parser-details</param-name>

<param-value>0</param-value>

</init-param>

<!-- Tiles Servlet parameter

Specify if xml parser should validate the Tiles configuration file.

true : validate. DTD should be specified in file header.

false : no validation

-->

<init-param>

<param-name>definitions-parser-validate</param-name>

<param-value>true</param-value>

</init-param>

<!-- Struts configuration, if Struts is used -->

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>validate</param-name>

<param-value>true</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>

<!-- Action Servlet Mapping -->

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>60</session-timeout>

</session-config>

<!-- The Welcome File List -->

<welcome-file-list>

<welcome-file>login.jsp</welcome-file>

</welcome-file-list>

<!-- Struts Tag Library Descriptor -->

<taglib>

<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-tiles.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-bean.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

</taglib>

<!--database resource configuration-->

<resource-ref>

<description>zchFive database connection pool </description>

<res-ref-name>jdbc/zchFiveDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

</web-app>

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">

<!--

This is the Struts configuration file for the example application,

using the proposed new syntax.

NOTE: You would only flesh out the details in the "form-bean"

declarations if you had a generator tool that used them to create

the corresponding Java classes for you. Otherwise, you would

need only the "form-bean" element itself, with the corresponding

"name" and "type" attributes.

-->

<struts-config>

<!-- ========== Form Bean Definitions =================================== -->

<form-beans>

<form-bean name="userForm" type="UserForm">

</form-bean>

</form-beans>

<!-- ========== Global Forward Definitions ============================== -->

<global-forwards>

</global-forwards>

<!-- ========== Action Mapping Definitions ============================== -->

<action-mappings>

<action path="/execute"

type="UserAction"

name="userForm"

scope="request"

validate="false">

<forward name="err" path="/error.jsp"/>

<forward name="ok" path="/success.jsp"/>

</action>

</action-mappings>

</struts-config>

输入数据的页面

in.jsp

<%@ page contentType="text/html; charset=gb2312" %>

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

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

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

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

<html:html>

<head>

<title>Login</title>

</head>

<body>

<center>

<pre>

<html:form action="execute.do" method="post">

<h3> 请输入 </h3>

FirstName: <html:text property="str1" size="12"/>

LastName: <html:text property="str2" size="12"/>

<html:submit value="提交"/> <html:reset value="重置"/>

</html:form>

</pre>

</center>

</body>

</html:html>

显示正确结果的页面

success.jsp

<%@ page contentType="text/html; charset=gb2312" %>

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

<html>

<center>

Your name:<bean:write name="userForm" property="strAdd"/>

</center>

</html>

提示警告的页面

error.jsp

<%@ page contentType="text/html; charset=gb2312" %>

<html>

<center>

error!你没有输入 FirstName或者 LastName

</center>

</html>

好了,下面到程序员真正该做的工作了,那就是写java代码啊。

一,不要问在什么地方写java代码

二,不要问用什么办法把java代码编译成类文件

三,不要问把java文件和类文件放在什么地方

……这样的问题我不会再回答。

你千万不要忘了要导入两个JAR包啊

用来保存数据的form

UserForm.java

import org.apache.struts.action.*;

public class UserForm extends ActionForm

{

private String str1 = null;

private String str2 = null;

private String strAdd = null;

public String getStr1()

{

return str1 == null?"":str1.trim();

}

public void setStr1(String str1)

{

this.str1 = str1;

}

public String getStr2()

{

return str2 == null?"":str2.trim();

}

public String getStrAdd()

{

return strAdd;

}

public void setStr2(String str2)

{

this.str2 = str2;

}

public void setStrAdd(String strAdd)

{

this.strAdd = strAdd;

}

}

看见了form了吧,没什么神气的,就是一些属性和关于属性的getset方法

用来处理数据的action

UserAction.java

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

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.actions.DispatchAction;

public class UserAction extends DispatchAction

{

public ActionForward execute( ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception

{

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值