起初的工程(未引入struts)目录结构如下:
修改你的web.xml配置
如下:
修改之前是:
加入:
引入struts所需的标签库
引入目录结构如下:(配置文件描述)
在工程中引入struts工程所需的.jar文件
目录结构如下:
引入资源文件:
applicationresources.properties
内容如下:
# -- buttons --button.submit=submitbutton.cancel=cancelbutton.confirm=confirmbutton.reset=resetbutton.save=save
现在编写一个测试代码:
编写一个提交数据库的表单jsp文件usestruts.jsp:
如下:
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"><%@ page language="java" contenttype="text/html; charset=utf-8" %><%@ taglib url="/web-inf/struts-html.tld" prefix="html" %><%@ taglib url="/web-inf/struts-bean.tld" prefix="bean" %>
用户:
密码:
标题:
内容:
作者:
name="usestrutsform"
scope="request"
input="/jsp/usestruts.jsp"
validate="true">
创建两个action:
preusestrutsaction.java 、processusestrutsaction.java
preusestrutsaction.java:
代码如下:
/* * $header: d:/cvs/repository/struts-examples/web/040content/web-inf/src/java/examples/successaction.java,v 1.2 2003/06/13 06:23:13 sraeburn exp $ * $revision: 1.2 $ * $date: 2005/11/25 06:23:13 $ * this software consists of voluntary contributions made by many * individuals on behalf of the apache software foundation. for more * information on the apache software foundation, please see *
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.action;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
/** * * @author huhpreal * @version $revision: 1.2 $ $date: 2003/06/13 06:23:13 $ */public class preusestrutsaction extends action {
// ------------------------------------------------------------constructors
/**
* constructor for successaction.
*/
public preusestrutsaction() {
super();
}
// ---------------------------------------------------------- action methods
/**
* @param mapping the actionmapping used to select this instance
* @param form the optional actionform bean for this request (if any)
* @param request the http request we are processing
* @param response the http response we are creating
*
* @exception exception if mapping.findforward throws an exception
*
* @return the "success" actionforward, or null if it cannot be found
*/ public actionforward execute(
actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
return mapping.findforward("success");
}}
processusestrutsaction.java:
代码如下:
/* * $header: d:/cvs/repository/struts-examples/web/040content/web-inf/src/java/examples/dyna/processdynaaction.java,v 1.3 2003/06/22 04:51:04 sraeburn exp $ * $revision: 1.3 $ * $date: 2005/11/25 04:51:04 $ * * this software consists of voluntary contributions made by many * individuals on behalf of the apache software foundation. for more * information on the apache software foundation, please see * */package action;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.action;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.dynaactionform;
import bean.linkdb;
/** * retrieve and process data from the submitted form
* * @author huhpreal * @version $revision: 1.3 $ $date: 2005/11/25 04:51:04 $ */public class processusestrutsaction extends action {
// ------------------------------------------------------------ constructors
/** * constructor for processoptionsaction.
*/
public processusestrutsaction() {
super();
}
/**
* @param mapping the actionmapping used to select this instance
* @param form the optional actionform bean for this request (if any)
* @param request the http request we are processing
* @param response the http response we are creating
*
* @exception exception if the application logic throws an exception
*
* @return the actionforward for the next view
*/
public actionforward execute(
actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
system.out.println("enter into processusestrutsaction");
if (iscancelled(request)) {
return mapping.findforward("home");
}
/**
* 获取表单信息
* 这里最好用bean实体封装
* 时间关系就不提取
*/
string user=new string("");
string password=new string("");
string title=new string("");
string content=new string("");
string author=new string("");
user=(string) ((dynaactionform) form).get("user");
password=(string) ((dynaactionform) form).get("password");
title=(string) ((dynaactionform) form).get("title");
content=(string) ((dynaactionform) form).get("content");
author=(string) ((dynaactionform) form).get("author");
/***
* 数据库操作
*/
linkdb linkdb=new linkdb();
string sqlstr="insert into conad (title,content,author) values('"+title+"','"+content+"','"+author+"')";
linkdb.executeupdate(sqlstr);
// forward to result page
return mapping.findforward("success");
}}
测试结果:
测试成功!
闽公网安备 35060202000074号