最简单的表单提交(struts1.1实现)
技术 2009-07-25 17:57:24 阅读106 评论0 字号:大中小 订阅
一.建立工程
1.创建一个Tomcat工程,起名字叫test。
2.在根目录下创建index.jsp和jsp文件夹。jsp文件夹里创建一个test.jsp的文件。
3.在WEB-INF文件夹下创建struts-config.xml和web.xml文件。并将struts所附带的标签struts-bean.tld,struts-html.tld和struts-logic.tld拷入。
4.将struts附带的struts.jar以及10个common-*.jar的文件拷入lib文件夹下。
5.在src文件夹下创建action和form文件夹。分别在这两个文件夹下创建ButtonAction.java和ButtonForm.java。
二.添加程序
1.index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<title>
test Hellozhu!
</title>
</head>
<body>
<html:form action="/buttontest">
<table>
<tr>
<td>
<html:text property="name" size="40" maxlength="50"/>
</td>
<td>
<html:submit value="apply"/>
</td>
</tr>
</table>
</html:form>
</body>
</html>
2. test.jsp
<%@ page language="java" contentType="text/html; charset=windows-31j"
pageEncoding="windows-31j"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>test1</title>
</head>
<body>
Mr. <bean:write name="btform" property="name"/>, welcome!
</body>
</html>
3.struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="btform" type="form.ButtonForm"></form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/buttontest"
type="action.ButtonAction"
name="btform"
input="index.jsp">
<forward name="success" path="/jsp/test.jsp"/>
</action>
</action-mappings>
</struts-config>
4.web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<display-name>Struts Blank Application</display-name>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>