关闭

Struts(JFIS)

536人阅读 评论(0) 收藏 举报
 
 
Token验证
public ActionForward model_new_url(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {
                ModelListDAO md = new ModelListDAO();
            request.setAttribute("models", md.list());
            //第一步放一个Token
            this.saveToken(request);
                return mapping.findForward("model_new_url");
        }
 
public ActionForward model_insert(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {
                ModelForm modelForm = (ModelForm) form;
                ModelBean mb = new ModelBean();
               
                //第二步,判断Token是否存在(是否重复提交)
                if(this.isTokenValid(request)){
                        this.resetToken(request);
                ModelDAO md = new ModelDAO();
                md.model_insert(mb);
               
                return mapping.findForward("model_insert");
                }else{
                        request.setAttribute("flag", "modle");
                        return mapping.findForward("error");
                }
        }
 
 
 
 
 
 
 
 
 
Validation验证(框架验证)
注意:要继承可以验证的form
struts-config.xml中加入插件
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
 </plug-in>
 
 
ApplicationResources.properties文件
 
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project ValidateTest
userName=yonghumingbunengweikong!
 
<html>
<head>
<title>JSP for UserForm form</title>
</head>
<body>
<html:form action="/user">
userName : <html:text property="userName"/>
<html:errors property="userName"/><br/>
userPwd : <html:text property="userPwd"/>
<html:errors property="userPwd"/><br/>
       <html:submit/><html:cancel />
</html:form>
</body>
</html>
 
validation.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
 
 <formset>
                <form name="userForm">   //要验证的form的名字
                <field property="userName" depends="required">
                <!-- field中 depends表示validator-rulers.xml中以定义好的方法 -->
                <msg name="required" key="userName" resource="true" />
                <!-- msg中 resource表示是否从properties文件中读取提示信息 -->
                <!-- 当resource="true"时,直接从properties文件中读值-->
                </field>
                <field property="userPwd" depends="required,minlength">
                <msg name="required" key="mima bu neng wei kong !"
                                resource="false" />
            <!-- 当resource="false"时,直接输出key的值-->
        <msg name="minlength" key="chang du bu neng xiao yu 5!"resource="false" />
                        <var>
                                <var-name>minlength</var-name>
                                <var-value>5</var-value>
                        </var>
                </field>
                <field property="userPwd" depends="required">
        <msg name="required" key="mima bu neng wei kong !" resource="false"/>
                        </field>
                </form>
</formset>
</form-validation>       
 
 
<html:cancel />标签的使用
 Action类里的代码
public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {
                ValidatorForm testForm = (ValidatorForm) form;
                //<html:cancel />标签的使用
                if(this.isCancelled(request))
                {
                        return mapping.findForward("cancel");
                }else
                {
                        returnnull;
                }
               
        }
jsp页面见到此标签,在Action类里只要写上面代码就可以了。
 
动态Form(写在struts-config.xml里)
<form-beans >
    <form-bean name="modelForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="modelName" type="java.lang.String" />
      <form-property name="parentModelId" type="java.lang.String" />
      <form-property name="modelDesc" type="java.lang.String" />
      <form-property name="modelPath" type="java.lang.String" />
    </form-bean>
    <form-bean name="powerForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="powerDesc" type="java.lang.String" />
      <form-property name="modelId" type="java.lang.String" />
      <form-property name="powerName" type="java.lang.String" />
    </form-bean>
</form-beans>
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:418560次
    • 积分:5693
    • 等级:
    • 排名:第4755名
    • 原创:141篇
    • 转载:120篇
    • 译文:4篇
    • 评论:30条
    最新评论