二.struts中使用 DynaActionForm
使用表单是网页里面经常遇到的事情.利用struts的标签可以很好的完成这一功能.在struts中表单分为两种.一种是ActionForm..另外一种是DynaActionForm.所谓动态是在使用表单时不需要创建对就的bean文件.ActionForm很简单,在此只说说DynaActionForm.因为在这个小项目中在很多的用户输入.数据验证部分我放到了Action中进行.于是在表示层Form中只是接收数据.用DynaActionForm实现是个很好的选择.
index.jsp
<TABLE border="0" width="100%" height="40px">
<TR>
<TD >
<html:form action="/selectAction" method="POST">
选择文法:
<html:select property="select_grammar" >
<html:options collection="grammarCollection"
property="label"
labelProperty="label"/>
</html:select>
<p>
输入文法:
<logic:present name="SELECTED_GRAMMAR" scope="request">
<span id="thegrammar">
<html:textarea cols="40"value="${SELECTED_GRAMMAR}">
</html:textarea>*
</span>
</logic:present>
<p>
<html:checkbox property="first" value="true" />:求first集合 <
br>
<html:checkbox property="follow" value="true" />:求follow集合<
br>
<html:checkbox property="select" value="true" />:求select集合 <
br>
<html:submit/>
<html:reset/>
</html:form>
</TD>
</TR>
</TABLE>
这个页面中和使用静态表单没什么区别,只是在struts-config.xm配置稍微有些不同.
<form-bean name="selectForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="select_grammar" type="java.lang.String" />
<form-property name="selected_grammar" type="java.lang.String" />
<form-property name="first" type="java.lang.Boolean" />
<form-property name="select" type="java.lang.Boolean"/>
<form-property name="follow" type="java.lang.Boolean"/>
</form-bean>
对应此表单的Action的配置:
<action path="/selectAction"
type="ysu.cs.icai.action.selectAction"//定义的处理提交的类
input="/index.jsp"
name="selectForm"
validate="false"
scope="request">
<forward name="SUCCESS_PATH" path="/selectindex.do"/>
</action>
和ActionForm的不同之处还在于在Action中获取提交的参数时要将ActionForm强制转换为DynaActionForm:
DynaActionForm analyzeForm = (DynaActionForm) form;
如何获取各参数如下:
import org.apache.struts.action.DynaActionForm;
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;
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public class selectAction extends Action ...{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) ...{
DynaActionForm analyzeForm = (DynaActionForm) form;//强制
String grammar=(String)analyzeForm.get("selected_grammar");
Boolean first=(Boolean)analyzeForm.get("first");//
Boolean follow=(Boolean)analyzeForm.get("follow");//
Boolean select=(Boolean)analyzeForm.get("select");//获取提交的各个参数.
//......something about what you what todo.........
return mapping.findForward("SUCCESS_PATH");
}
}
动态表单有很多优点.比如可以实现跨页的表单提交.还过也有缺点比如此时它就不能将validate框架集成到里面.有个方法就是可心自己继承DynaActionForm类在其中添加validate方法.再将实际的应用中的Form继承此类.不过此时的将失去使用DynaActionForm的灵活性.只好自己权衡了.
三.struts中使用监听器类
监听器类顾名思义就是.某些事件的触发下能执行的动作.在servlet 中有这样的类专门监听网页中的动作.整理好下:
监听器种类
监听接口
监听对象
实现方法
ServletContextAttributeListener
监听ServletContext的属性的操作
比如增加、删除、修改属性。
ServletContextListener
监听ServletContext
当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;
当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener
监听HttpSession的操作
当创建一个Session时,激发session Created(HttpSessionEvent se)方法;
当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener
监听HttpSession中的属性的操作
当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;
当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;
当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
当然还有关于request对象操作的监听器类.在此不在敖述;下面举一个我使用的例子:使用监听器实现对数据库的访问,当应用启动时即访问数据库加载信息,监听器类如下:
package ysu.cs.icai.listener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import org.apache.struts.util.LabelValueBean;
import ysu.cs.icai.database.ConDatabase;
/** *//**
* 监听器类设置数据源.在初始化web.xml文件时加载;
*/
public class ResourceInitListener implements ServletContextListener...{
public void contextInitialized(ServletContextEvent init) ...{
ServletContext application = init.getServletContext();
String driverClass = application.getInitParameter("driverClass");
String jdbcURL = application.getInitParameter("jdbcURL");//获取web.xml中配置的参数.
Vector grammarCollection=new Vector();
java.sql.Connection myConnection=null;
java.sql.Statement st;
ResultSet set;
String sql1="select * from GRAMMAR";
ConDatabase dataSource = new ConDatabase();
try ...{
dataSource.setDriver(driverClass, jdbcURL);
application.setAttribute("DATASOURCE",dataSource);
//System.out.print("ATOM:数据初始化成功!");
} catch(ClassNotFoundException e) ...{
e.printStackTrace();
}
try...{
myConnection=dataSource.getConnection();
st=myConnection.createStatement();
st.execute("use icaidb;");
System.out.print("ATOM:change to use icaidb!");
set=st.executeQuery(sql1);
while(set.next())...{
grammarCollection.add(new LabelValueBean(set.getString(1),set.getString(2)));
}
//System.out.print("ATOM:数据检索成功!");
}catch(SQLException er)...{
er.printStackTrace();
//System.out.print("ATOM:数据库连接异常!");
}
if(grammarCollection!=null)...{
application.setAttribute("grammarCollection",grammarCollection);
}
}
public void contextDestroyed(ServletContextEvent destroy) ...{
ServletContext application = destroy.getServletContext();
}
}
为了使用启动应用时能调用到这个文件还必须在web.xml为监听器进行配置.
<context-param>
<param-name>driverClass</param-name>
<param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
</context-param>
<context-param>
<param-name>jdbcURL</param-name>
<param-value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=icaidb;user=admin;password=admin</param-value>
<!--jdbcURLl和driverClass是为ysu.cs.icai.listener.ResourceInitListener配置的两个参数-->
</context-param>
<listener>
<listener-class>ysu.cs.icai.listener.ResourceInitListener</listener-class>
</listener>
这个文件启动后ServletContext中的参数grammarCollection会被保存到ServletContext域中.在jsp或action文件中可使用getServletContext().getAttribute("grammarCollection")访问这个对象.
<html:select property="select_grammar">
<html:options collection="grammarCollection"
property="laber"
labelProperty="value"/>
</html:select>
以上代码可以生成一个从数据库动态检索列表项的下拉列表.因为grammarCollection是一个Vector类型的对象.可以这样为其添加值对:
grammarCollection.add(new LabelValueBean("laber1","value1");
关于lisener的东西还很多.偶只是用到这么多,欢迎留言交流.
icai项目开发日记(三)....struts中Ajax的简单应用.....
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。