服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

struts配置说明

struts配置说明


首先新建一个web项目
 
取个项目名称
 
选择项目,右键,myeclipse->add struts capabilities

 
出现画面:
 
此界面是在自动配置struts的配置文件 可以根据需要配置信息.
其中default application resource :指的是消息资源的配置
 
配置完成之后,会出现左侧列表中的文件,其中web.xml如下:
在 actionservlet 的 web.xml 中使用调试级别来确保获得扩展的调试消息。有效的 debug 值是 0(不记录)到 6(最严重)。 将 detail 参数设置为调试来自 digestor 的消息.
 
 
struts-config.xml文件如下:
 
其中data-sources用于配置数据库连接池 
form-beans用于配置actionform
global-exceptions用于配置全局异常处理
global-forwards用于配置全局forward
action-mappings 用于配置action映射;
message-resources 用于配置消息资源,也就是上面提到过的default application resource
 
下面开始正式编写程序;以做一个查询为例:
 
假设现在我们已经知道了数据库结构和查询条件.
数据库结构为
 
t_user


















字段名

类型

长度

中文诠释

备注

userid

char

10

用户id

主键

username

varchar

20

用户名

 

查询条件:通过userid精确查找,通过username模糊搜索
注意,为了小组开发能够顺利进行,在命名标识符时请遵循标识符的一些约定,譬如此处变量名第一个字母小写,第二个单词的第一个字母大写等等
 
首先可以肯定查询条件中会存在userid和username两个字段,所以我们可以先新建一个actionform,过程如下:
找到你想存放该java类的包目录,右键,新建,类
出现画面:
 
定义类名,尽量使其有意义,而且一眼就能看懂,然后选择超类,继承
org.apache.struts.action.actionform类,然后点击完成
然后在类中声明两个变量userid和username,以及最终需要返回的内容,这个内容在这里应该时一个列表;我们约定所有的查询结果如果返回时列表的都命名为results,如果时单一内容的都命名为result,这样有助于小组开发的时候减少后期加工
然后找到菜单中的源代码,选择生成getter和setter方法
出现界面:
选择全部选中,单击确定;
然后覆盖父类actionform的reset()方法
      
       public void reset(actionmapping mapping,httpservletrequest request){
              userid=null;
              username=null;
              results=null;
       }
此方法用于当一个请求处理完之后,清除或者重置actionform的数据.
 
actionform 还包含一个名为 validate() 的方法。可以使用 validate 方法验证表单项,如果验证失败,则填充 actionerrors 对象,并将控制权返还给调用页面,随后调用页面会显示这些错误。
 
定义完actionform之后可以定义一个返回的results里面存放的bean,新建一个类
/*
 * 创建日期 2005-10-26
 *
 * todo 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - java - 代码样式 - 代码模板
 */
package com.yourcompany.struts;
 
/**
 * @author admin
 *
 * todo 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - java - 代码样式 - 代码模板
 */
public class user {
       private string userid;
       private string username;
      
      
 
       /**
        * @return 返回 userid。
        */
       public string getuserid() {
              return userid;
       }
       /**
        * @return 返回 username。
        */
       public string getusername() {
              return username;
       }
       /**
        * @param userid 要设置的 userid。
        */
       public void setuserid(string userid) {
              this.userid = userid;
       }
       /**
        * @param username 要设置的 username。
        */
       public void setusername(string username) {
              this.username = username;
       }
}
 
      
 
 
之后可以新建一张jsp页面
 

页面内容如下

<%@ page language="java" contenttype="text/html; charset=gb2312"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">

<html:html locale="true">

  <head>

    <html:base />

    <title>usersearch.jsp</title>

    <meta http-equiv="content-type" content="text/html; charset=gb2312">

  </head>

  <body>

    <html:form action="/usersearch" method="post">

    <table border="0">

        <tr>

            <td align="right"> 姓名:</td>

            <td><html:text property="name"/></td>

        </tr>

        <tr>

            <td></td>

            <td>-- or --</td>

        </tr>

        <tr>

            <td align="right">

                用户id:

            </td>

            <td><html:text property="ssnum"/>(xxx-xx-xxxx)</td>

        </tr>

        <tr>

            <td></td>

            <td> <html:submit  /></td>

        </tr>

    </table>

</html:form>


 

<logic:present name="userform" property="results">

    <hr width="100%" size="1" noshade="noshade">

   

    <bean:size id="size" name="userform" property="results"/>

    <logic:equal name="size" value="0">

        <center> <font color="red"><b>no employee found</font></center>

    </logic:equal>

    <logic:greaterthan name="size" value="0">

        <table border="1">

            <tr>

                <th>姓名

                </th>

                <th>用户id</th>

            </tr>

            <logic:iterate id="result" name="userform" property="results">

                <tr>

                    <td><bean:write name="result" property="username"/> </td>

                    <td><bean:write name="result" property="userid"/> </td>

                </tr>

            </logic:iterate>

        </table>

       

    </logic:greaterthan>


 

</logic:present>

</body>

</html:html>
这个jsp页面中有一个form,
 

<html:form action="/usersearch " method="post">
他的action是/usersearch; 所以我们可以在struts-config.xml中定义这个action;
由于此action用到了一个actionform叫userform,我们可以同时配置form-beans配置如下:
 

<?xml version="1.0" encoding="utf-8"?>

<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

   <data-sources />

   <form-beans >

        <form-bean type="com.yourcompany.struts.userform" name="userform" >

           

        </form-bean>

   </form-beans>

   <global-exceptions />

   <global-forwards />

   <action-mappings >

   <action path="/usersearch" input="usersearch.jsp" scope="request" name="userform" ></action>

   </action-mappings>

   <message-resources parameter="com.yourcompany.struts.applicationresources" />
</struts-config>
 
这里的action还差一个内容,就是action所指向的java类,我们可以添加一个action,取名叫usersearchaction,并且继承org.apache.struts.action.action类
他的内容,通过覆盖action的execute方法,进行操作,最终得到结果
 
 

然后我们在struts-config.xml中添加这个类: (加粗的部分);
   <action path="/usersearch" input="usersearch.jsp" scope="request" name="userform" type="com.yourcompany.struts.usersearchaction"></action>
 
在usersearchaction.java中,我们可以通过getdatasource(httpserverletrequest request)方法得到数据库连接池,数据库连接池的配置如下:
找到struts-config.xml文件,修改data-sources标签

<data-sources>

      <data-source type="org.apache.struts.util.genericdatasource">…数据库连接池

         <set-property property="password" value="lpl**#@)()#rjxy" /> ……….密码

         <set-property property="mincount" value="5" />…………………...最小连接数

         <set-property property="maxcount" value="10" />…………………..最大连接数

         <set-property property="user" value="sa" />………………………..用户名

         <set-property property="driverclass" value="com.microsoft.jdbc.sqlserver.sqlserverdriver" />………………….驱动程序

         <set-property property="description" value="" />………………描述

         <set-property property="url" value="jdbc:microsoft:sqlserver://localhost:1433;databasename=rongda;selectmethod=cursor" /> ……………………………………………………………………………连接地址

         <set-property property="readonly" value="false" />       …..是否只读

         <set-property property="autocommit" value="false" />….是否自动提交事务

         <set-property property="logintimeout" value="" />   ……登陆超时

      </data-source>

  
   </data-sources>
 
 
然后程序就可以运行了
 

扫描关注微信公众号