需求是点开页面,在下拉列表里就已经有值了,那么首先就要一个超链接:
<html:link action="/SelectDynamic">收费统计</html:link>//提交SelectDynamic到struts-config.xml的path里,跟action="SelectDynamic.do"一个意思
SelectDynamicForm
package struts.form;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class SelectDynamicForm extends ActionForm {
private static final long serialVersionUID = 1L;
private String chargeStatPersonnelSelect;
private String chargeStatChargeItemSelect;
private String chargeStatManageDepartmentSelect;
private List errors ;
public void setErrors(List errors)
{
this.errors = errors ;
}
public List getErrors()
{
return this.errors ;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// 判断用户是否登陆,没有的话返回到登陆页面
if (request.getSession().getAttribute("sessionUserName")==null||"".equals(request.getSession().getAttribute("sessionUserName")))
{
mapping.findForward("sessionEnded") ;
}
else
{
}
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public String getChargeStatChargeItemSelect() {
return chargeStatChargeItemSelect;
}
public void setChargeStatChargeItemSelect(String chargeStatChargeItemSelect) {
this.chargeStatChargeItemSelect = chargeStatChargeItemSelect;
}
public String getChargeStatManageDepartmentSelect() {
return chargeStatManageDepartmentSelect;
}
public void setChargeStatManageDepartmentSelect(
String chargeStatManageDepartmentSelect) {
this.chargeStatManageDepartmentSelect = chargeStatManageDepartmentSelect;
}
public String getChargeStatPersonnelSelect() {
return chargeStatPersonnelSelect;
}
public void setChargeStatPersonnelSelect(String chargeStatPersonnelSelect) {
this.chargeStatPersonnelSelect = chargeStatPersonnelSelect;
}
}
SelectDynamicAction
package struts.action;
import java.util.List;
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 struts.form.SelectDynamicForm;
import util.SelectDynamicPager;
import bean.SQLBean;
public class SelectDynamicAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
//判断用户退出后不允许进行操作,返回至登陆页面
if (request.getSession().getAttribute("sessionUserName")==null||"".equals(request.getSession().getAttribute("sessionUserName")))
{
return mapping.findForward("sessionEnded");
}
else
{
}
SelectDynamicForm selectDynamicForm = (SelectDynamicForm) form;
// 收费人员 czy dzjc.dzjc_sfje
// 收费项目 sfxmmc dzjc.dzjc_sfje
// 管理部门 glbm dzjc.dzjc_sfje
String chargeStatPersonnelSelect = selectDynamicForm.getChargeStatPersonnelSelect() ;
String chargeStatChargeItemSelect = selectDynamicForm.getChargeStatChargeItemSelect() ;
String chargeStatManageDepartmentSelect = selectDynamicForm.getChargeStatManageDepartmentSelect() ;
List list;
SelectDynamicPager selectDynamicPager ;
if(chargeStatPersonnelSelect==null||chargeStatChargeItemSelect==null||chargeStatManageDepartmentSelect==null)
{
try
{
SQLBean sb = new SQLBean();
list=sb.sqlSelectDynamic();
selectDynamicPager = new SelectDynamicPager(list);
list=selectDynamicPager.getPageList();
request.setAttribute("list", list);
request.setAttribute("SelectDynamic", selectDynamicPager);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return mapping.findForward("ChargeStatSucceed");
}
}
SelectDynamicPager
package util;
import java.util.ArrayList;
import java.util.List;
import vo.SelectDynamicPagerVo;
public class SelectDynamicPager
{
@SuppressWarnings("unused")
private List list;
public SelectDynamicPager(List list)
{
this.list=list;
}
@SuppressWarnings("unchecked")
public List getPageList()
{
List templist=new ArrayList();
int j=0;
for(int i=0;i<list.size();i++)
{
SelectDynamicPagerVo selectDynamicPagerVo=(SelectDynamicPagerVo)list.get(i);
templist.add(j,selectDynamicPagerVo);
j++;
}
return templist;
}
public String toString(int temp)
{
return Integer.toString(temp);
}
}
SelectDynamicPagerVo
//要显示在jsp里的字段
package vo;
public class SelectDynamicPagerVo
{
private String czy;
private String sfxmmc;
private String glbm;
public String getCzy() {
return czy;
}
public void setCzy(String czy) {
this.czy = czy;
}
public String getGlbm() {
return glbm;
}
public void setGlbm(String glbm) {
this.glbm = glbm;
}
public String getSfxmmc() {
return sfxmmc;
}
public void setSfxmmc(String sfxmmc) {
this.sfxmmc = sfxmmc;
}
}
jsp
<html:form action="/ChargeStat.do" method="post">//select必须在form里
<html:select property="chargeStatChargeItemSelect" οnchange="ChargeStatJavaScript('chargeStatSelectTwo');">
<html:option value=""></html:option>
<html:optionsCollection name="list" value="sfxmmc" label="sfxmmc" />
</html:select>
</form>
<script language="javascript" type="text/javascript">
function ChargeStatJavaScript(chargeStatButton)
{
var selectOne = 'chargeStatSelectOne' ;
var selectTwo = 'chargeStatSelectTwo' ;
var selectThree = 'chargeStatSelectThree' ;
if(chargeStatButton==selectOne)
{
document.all["chargeStatPersonnelInput"].value = document.all["chargeStatPersonnelSelect"].value
}
else if(chargeStatButton==selectTwo)
{
document.all["chargeStatChargeItemInput"].value = document.all["chargeStatChargeItemSelect"].value
}
else if(chargeStatButton==selectThree)
{
document.all["chargeStatManageDepartmentInput"].value = document.all["chargeStatManageDepartmentSelect"].value
}
else
{
document.write('error!请检查');
}
}
</script>
struts-config.xml
<form-bean name="SelectDynamicForm" type="struts.form.SelectDynamicForm" /> <action attribute="SelectDynamicForm" input="/ChargeStat.jsp" name="SelectDynamicForm" path="/SelectDynamic" scope="request" type="struts.action.SelectDynamicAction" validate="false"> <forward name="ChargeStatSucceed" path="/ChargeStat.jsp" /> </action>
SQL
public List sqlSelectDynamic()
{
List list = new ArrayList();
// 声明一个SQL变量,用于保存SQL语句
String sql = null ;
// 编写SQL语句
sql = "select czy,sfxmmc,glbm from dzjc.dzjc_sfje " ;
try
{
// 实例化数据库操作对象
pstmt = dbc.getConnection().prepareStatement(sql) ;
System.out.println("操作对象已被实例化");
System.out.println(sql);
// 查询记录
rs = pstmt.executeQuery() ;
System.out.println("执行查询完毕");
while (rs.next())
{
SelectDynamicPagerVo selectDynamicPagerVo = new SelectDynamicPagerVo();
//这个顺序控制分页jsp显示的顺序
selectDynamicPagerVo.setCzy(rs.getString(1));
selectDynamicPagerVo.setSfxmmc(rs.getString(2));
selectDynamicPagerVo.setGlbm(rs.getString(3));
list.add(selectDynamicPagerVo);
}
// 依次关闭
rs.close() ;
pstmt.close() ;
}
catch(Exception e)
{
System.out.println(e) ;
}
finally
{
// 最后一定要保证数据库已被关闭
dbc.close() ;
}
return list ;
}