首页 > 开发 > 综合 > 正文

如何在Web工程项目中使用Struts

2024-07-21 02:15:03
字体:
来源:转载
供稿:网友
  起初的工程(未引入struts)目录结构如下:
  
  修改你的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><!-- the mapping for the default servlet --><servlet><servlet-name>estservlet</servlet-name><servlet-class>action.jdbcservlet</servlet-class></servlet><servlet-mapping><servlet-name>estservlet</servlet-name><url-pattern>/estservlet</url-pattern></servlet-mapping><servlet><servlet-name>delconad</servlet-name><servlet-class>action.delconadbean</servlet-class></servlet><servlet-mapping><servlet-name>delconad</servlet-name><url-pattern>/delconad</url-pattern></servlet-mapping></web-app>
  
  加入:
  <!-- import struts mvc framework--><!-- standard action servlet configuration 标准struts action servlet 配置--><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>
  <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>
  <init-param>
  <param-name>validate</param-name>
  <param-value>true</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup></servlet><!-- standard action servlet mapping 标准struts .do请求--><servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><!--struts tag library descriptors struts 标签--><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>
  <taglib>
  <taglib-uri>/web-inf/struts-template.tld</taglib-uri>
  <taglib-location>/web-inf/struts-template.tld</taglib-location>
  </taglib>
  <taglib>
  <taglib-uri>/web-inf/struts-nested.tld</taglib-uri>
  <taglib-location>/web-inf/struts-nested.tld</taglib-location>
  </taglib>
  <taglib>
  <taglib-uri>/web-inf/struts-tiles.tld</taglib-uri>
  <taglib-location>/web-inf/struts-tiles.tld</taglib-location>
  </taglib>
  
  引入struts所需的标签库
  
  引入目录结构如下:(配置文件描述)
  
  在工程中引入struts工程所需的.jar文件
  
  目录结构如下:
  
  引入资源文件:
  applicationresources.properties
  
  内容如下:
  # -- buttons --button.submit=submitbutton.cancel=cancelbutton.confirm=confirmbutton.reset=resetbutton.save=save
  
  现在编写一个测试代码:
  
  编写一个提交数据库的表单jsp文件usestruts.jsp:
  
  如下:
  <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
  "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" %><html:html xhtml="true" locale="true"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>使用 struts dynaactionform 插入mysql数据库</title><html:base/><link rel="stylesheet" type="text/css" href="../css/example.css" /></head><body><html:errors/><html:form action="/insertmysql.do"><p>用户:<br/><html:text property="user" size="40" maxlength="20"/></p><p>密码:<br/><html:text property="password" size="40" maxlength="20"/></p><p>标题:<br/><html:text property="title" size="40" maxlength="50"/></p><p>内容:<br/><html:text property="content" size="40" maxlength="50"/></p><p>作者:<br/><html:text property="author" size="40" maxlength="50"/></p></html:form></body></html:html>修改struts-config.xml文件:<form-beans>
  <!-- dynaactionform bean for usesturts -->
  <form-bean name="usestrutsform" type="org.apache.struts.action.dynaactionform">
  <form-property name="user" type="java.lang.string" />
  <form-property name="password" type="java.lang.string" />
  <form-property name="title" type="java.lang.string" />
  <form-property name="content" type=" java.lang.string " />
  <form-property name="author" type="java.lang.string" />
  </form-bean>
  </form-beans><!-- ========== action mapping definitions ============================= -->
  <action-mappings>
  <!-- usersturts actionform ===================================== -->
  <action path="/prepareusestruts"
  type="action.preusestrutsaction">
  <forward name="success" path="/jsp/usestruts.jsp"/>
  </action>
  <action path="/processusestruts"
  type="action.processusestrutsaction"
  name="usestrutsform"
  scope="request"
  input="/jsp/usestruts.jsp"
  validate="true">
  <forward name="success" path="/jsp/usestruts.jsp"/>
  </action>
  </action-mappings>
  
  创建两个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 * <http://www.apache.org/>. * by huhpreal */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;
  /** * * @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");
  }}
  
  测试结果:
  
  测试成功!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表