1、前言
请求参数注入到XXXAction的属性,分2种情况,
1.提交表单时,请求参数是表单数据——》Action的属性。
2.不提交表单,请求参数在配置文件里配置——》Action的属性。
2、提交表单
【举例:注册账户】
1.请求页面
<form name="frmRequest" method="post" action="request.jsp" onsubmit="return doRequest(this);">
2.js文件
if (dirty) { alert("Some Required Fileds are missing or Invalid:\r\n" + msg); } else { $("#div_tips") .html( "<img src='/images/icon_loading.gif' border='0'>Process is in progress, please wait...") .toggle(); //提示正在处理;显示提示文字 $("#div_btn_request").toggle(); //隐藏提交按钮 var params = $(form).serialize(); //提交表单之前,对表单参数进行序列化 $(".tdErrMsg").html(""); $.post( form.action, params, function(json) { if (json.success) { $("#div_tips") .html( "<span style='color:339900'>Your request has been sent. We will confirm in 3 working days.</span>"); //如果正确提交页面,修改提示文字 $("#div_req_content").load("applyed.jsp", { cust : form.president.value }); //从服务器获取内容;传递请求参数 } else { $("#div_tips").html( "<b>" + json.errMsg + "</b>"); //如果提交页面出现错误,也要修改提示文字 $("#div_btn_request").toggle(); //显示提交按钮 for ( var prop in json) { //遍历json对象 try { $("#td_" + prop).html(json[prop]); //显示错误信息 } catch (e) { } } } }, "json"); } return false;}
3.配置文件
<fantasy>
<!-- 定义包 auth,处理客户登录等 -->
<package name='auth' extends='/' namespace='/auth' package='com.ppet.cust'
default='/json.jsp'>
<actions class='.AccountRequestAction'> <action name='account' handler='auth'> <result>account.jsp</result> </action> <action name='request' method='request' /> </actions>
4.XXXAction
/** * 注册会员 * @return 结果页面字符串 * @throws Exception */ public String request() throws Exception { json = SVC.validate(getModel()); if (json == null) { json = SVC.createAccount(f); } else { LOG.warn("Request Validation Fail: ".concat(json)); } return SUCCESS; }
5.业务逻辑类
3、不提交表单
【举例:查看所有产品(图片模式/列表模式)】
1.请求页面
<table width="100%" height="0%" cellspacing="0" cellpadding="0" border="0" class="xiaxian"> <tbody> <tr> <td width="98%" height="100%"><table width="100%" cellspacing="1" cellpadding="3" border="0"> <tbody> <tr bgcolor="b0a697" align='center'> <td class="prettypetMnu prettypetMnuLink" url='/member/design/index.jsp?custCate=${customerInfo.custCate}'>NEW DESIGN</td> <td class="prettypetMnu prettypetMnuLink" url='/member/prod/index.jsp?cate=&subCate=&custCate=${customerInfo.custCate}'>ALL DESIGN</td> <td class='prettypetMnu prettypetMnuLink' url='/member/promote/index.jsp?custCate=${customerInfo.custCate}'>PROMOTION</td> <td class='prettypetMnu prettypetMnuLink' url='/member/catalog/index.jsp'>CATALOG</td> <td class="prettypetMnu prettypetMnuLink" url='/member/index.jsp'>MY ACCOUNT</td> </tr> </tbody> </table></td> </tr> </tbody></table>
2.js文件
$(function(){ var href=window.location.pathname; var pos=href.lastIndexOf('/'); var path=href.substring(0,pos+1); var reg=new RegExp("^"+path+"[^/]+$","i"); var dirty=false; $("td.prettypetMnu").mouseover(function(){ if($(this).attr("class").indexOf("curr")==-1){ $(this).removeClass("prettypetMnuLink").addClass("prettypetMnuHover"); } }).mouseout(function(){ if($(this).attr("class").indexOf("curr")==-1){ $(this).removeClass("prettypetMnuHover").addClass("prettypetMnuLink"); } }).click(function(){ window.location=$(this).attr("url"); }).each(function(i,x){ if(reg.test($(this).attr("url"))) { $(this).removeClass("prettypetMnuLink").addClass("prettypetMnuHover").addClass("curr"); dirty=true; return false; } }); if(!dirty){ pos=path.indexOf("/",1); if(pos==-1){ return; } path=path.substring(0,pos+1); reg=new RegExp("^"+path+"[^/]+$","i"); $("td.prettypetMnu").each(function(i,x){ if(reg.test($(this).attr("url"))) { $(this).removeClass("prettypetMnuLink").addClass("prettypetMnuHover").addClass("curr"); return false; } }); } });
3.配置文件
<!-- 定义包 member,处理会员信息 --> <package name='bizProd' extends='member' extended-ns='prod' default='/json.jsp' package='com.ppet.prod'> <actions class='.CustProductAction'> <action name='index' handler='shell' method='list'> <param name='viewMode' value='IMG'/> //请求参数——》XXXAction的属性 <result>index.jsp</result> </action> <!-- <action name='listClothes' method='list'><result>listClothes.jsp</result></action> <action name='listBed' method='list'><result>listBed.jsp</result></action> <action name='listCarrier' method='list'><result>listCarrier.jsp</result></action> <action name='listHarness' method='list'><result>listHarness.jsp</result></action> <action name='viewProduct' method='edit'><result>viewProduct.jsp</result></action> --> <action name='list' method='list'><result>list.jsp</result></action> <action name='image' method='list'><result>image.jsp</result></action> <action name='view' method='edit' handler='prod'><result>view.jsp</result></action> <action name='quick-view' method='edit'><result>quick-view.jsp</result></action> <action name='retrieve' method='retrieve'/> <action name='listView' handler='prod' method='list'> <param name='viewMode' value='LIST'/> <result>index.jsp</result> </action> </actions> <action name='size-chart' handler='prod'><result>size-chart.html</result></action> <actions class='com.ppet.inv.CustStockAction'> <action name='stock-index' handler='prod' ><result>../stock/index.jsp</result></action> <action name='stock-list' ><result>../stock/list.jsp</result></action> </actions> </package>
4_1.表单类(JavaBean)
public String getListUrl(){ return isImgMode()?"image.jsp":"list.jsp"; }
4_2.父类
String formVar;
protected JsonMngActionSupport(String var, String cls) { formVar=var; if(!cls.contains(".")){ cls=getClass().getPackage().getName()+"."+cls; }else if(cls.startsWith(".")){ cls=getClass().getPackage().getName()+cls; } formClass=cls; }
/** * 创建搜索实例 * @return */ protected PagedSearchForm getForm() { if(searchForm!=null) { return searchForm; } /** * 必须定义了 formClass 才可以使用 */ if(formClass==null) { throw new UnsupportedOperationException("Unsupported Operation for "+getClass().getName()+" because no form class specified"); } // 获取存 String var=formVar; String ft=request.getParameter("formType"); if(ft!=null) { ActionFormType type=ActionFormType.convert(ft); var+="."+type.key; } HttpSession session=request.getSession(); PagedSearchForm form=(PagedSearchForm)session.getAttribute(var); if(form!=null){ searchForm=form; resetForm(form); return form; } ensureClassLoaded(); try { form=cls.newInstance(); session.setAttribute(var, form); searchForm=form; resetForm(form); return form; }catch(Exception e) { throw new IllegalArgumentException("Illegal Form class "+formClass+". It hasn't a public default Constructor",e); } }
4_3.Action
public String list() throws Exception { CustProdCondBuilder cb=new CustProdCondBuilder(); list(handler,cb,ProductOrderFactory.INST,lstAction); request.setAttribute("frmCustProdSearch", getModel()); HttpSession session = request.getSession(); System.out.println(session.getAttributeNames()); return SUCCESS; }
5.结果页面
<div id='div_prod_list' style='padding: 5px;'> <jsp:include page='${frmCustProdSearch.listUrl}' /></div>