struts1.X总结(30-40)

来源:互联网 发布:c语言教程谭浩强pdf 编辑:程序博客网 时间:2024/05/30 05:28

           <font color="red">没有符合条件的数据</font>

                  </td>

              </tr> 

              </logic:empty>

              <logic:notEmpty name="userlist">

                  <logic:iterate id="user" name="userlist">

                     <tr>

                         <td class="rd8">

                            <input type="checkbox" name="selectFlag" class="checkbox1"

                                value="<bean:write name="user" property="userId"/>">

                         </td>

                         <td class="rd8">

                            <bean:write name="user" property="userId" />

                         </td>

                         <td class="rd8">

                            <bean:write name="user" property="userName" />

                         </td>

                         <td class="rd8">

                            <bean:write name="user" property="contactTel" />

                         </td>

                         <td class="rd8">

                            <bean:write name="user" property="email" />

                         </td>

                         <td class="rd8">

                            <bean:write name="user" property="createDate" format="yyyy-MM-dd HH:mm:ss" />

                         </td>

                     </tr>

                  </logic:iterate>

              </logic:notEmpty>

           </table>

           <table width="95%" height="30" border="0" align="center"

              cellpadding="0" cellspacing="0" class="rd1">

              <tr>

                  <td nowrap class="rd19" height="2">

                     <div align="left">

                         <font color="#FFFFFF">&nbsp;&nbspxx&nbsp</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                         <font color="#FFFFFF">当前第</font>&nbsp

                         <font color="#FF0000">x</font>&nbsp

                         <font color="#FFFFFF"></font>

                     </div>

                  </td>

                  <td nowrap class="rd19">

                     <div align="right">

                         <input name="btnAdd" type="button" class="button1" id="btnAdd"

                            value="添加" onClick="addUser()">

                         <input name="btnDelete" class="button1" type="button"

                            id="btnDelete" value="删除" onClick="deleteUser()">

                         <input name="btnModify" class="button1" type="button"

                            id="btnModify" value="修改" onClick="modifyUser()">

                     </div>

                  </td>

              </tr>

           </table>

           <p>

              &nbsp;

           </p>

       </form>

 

Jsp输入页面

 

<html>

    <head>

       <meta http-equiv="Content-Type" content="text/html; charset=GB18030">

       <title>添加用户</title>

       <link rel="stylesheet" href="../style/drp.css">

       <script src="../script/client_validate.js"></script>

       <script type="text/javascript">

 

    function addUser() {

       if (document.getElementById("userId").value.length <4) {

           alert("用户代码不能小于4个字符!");

           document.getElementById("userId").focus();

           return;

       }

       var firstChar = document.getElementById("userId").value.charAt(0);

   

       if (!(firstChar >= 'a' && firstChar <='z')) {

           alert("用户代码首字符必须为字母");

           document.getElementById("userId").focus();

           return;

       }

       if(document.getElementById("userName").value == "") {

           alert("用户名称不能为空!");

           document.getElementById("userName").focus();

           return;

       }

       if (document.getElementById("password").value.length <6) {

           alert("输入的密码不能小于6位字符");

           document.getElementById("password").focus();

           return;

       }

       with (document.getElementById("userForm")) {

           method = "post";

           action = "add.do";

           submit();

       }

    }

 

    function goBack() {

       window.self.location ="list.do"

    }

   

    function init() {

       document.userForm.userId.focus();

    }

</script>

    </head>

 

    <body class="body1">

       <form name="userForm" target="_self" id="userForm">

           <div align="center">

              <table width="95%" border="0" cellspacing="2" cellpadding="2">

                  <tr>

                     <td>

                         &nbsp;

                     </td>

                  </tr>

              </table>

              <table width="95%" border="0" cellspacing="0" cellpadding="0"

                  height="25">

                  <tr>

                     <td width="522" class="p1" height="25" nowrap>

                         <img src="../images/mark_arrow_03.gif" width="14" height="14">

                         &nbsp;

                         <b>系统管理&gt;&gt;用户维护&gt;&gt;添加</b>

                     </td>

                  </tr>

              </table>

              <hr width="97%" align="center" size=0>

              <table width="95%" border="0" cellpadding="0" cellspacing="0">

                  <tr>

                     <td width="22%" height="29">

                         <div align="right">

                            <font color="#FF0000">*</font>用户代码:&nbsp;

                         </div>

                     </td>

                     <td width="78%">

                         <input name="userId" type="text" class="text1" id="userId"

                            size="10" maxlength="10">

                     </td>

                  </tr>

                  <tr>

                     <td height="26">

                         <div align="right">

                            <font color="#FF0000">*</font>用户名称:&nbsp;

                         </div>

                     </td>

                     <td>

                         <input name="userName" type="text" class="text1" id="userName"

                            size="20" maxlength="20">

                     </td>

                  </tr>

                  <tr>

                     <td height="26">

                         <div align="right">

                            <font color="#FF0000">*</font>密码:&nbsp;

                         </div>

                     </td>

                     <td>

                         <label>

                            <input name="password" type="password" class="text1"

                                id="password" size="20" maxlength="20">

                         </label>

                     </td>

                  </tr>

                  <tr>

                     <td height="26">

                         <div align="right">

                            联系电话:&nbsp;

                         </div>

                     </td>

                     <td>

                         <input name="contactTel" type="text" class="text1"

                            id="contactTel" size="20" maxlength="20">

                     </td>

                  </tr>

                  <tr>

                     <td height="26">

                         <div align="right">

                            email:&nbsp;

                         </div>

                     </td>

                     <td>

                         <input name="email" type="text" class="text1" id="email"

                            size="20" maxlength="20">

                     </td>

                  </tr>

              </table>

              <hr width="97%" align="center" size=0>

              <div align="center">

                  <input name="btnAdd" class="button1" type="button" id="btnAdd"

                     value="添加" onClick="addUser()">

                  &nbsp;&nbsp;&nbsp;&nbsp;

                  <input name="btnBack" class="button1" type="button" id="btnBack"

                     value="返回" onclick="goBack()" />

              </div>

           </div>

       </form>

    </body>

</html>

 

注意加入虚目录就可以解决图片的显示问题

user/list.do

 

13.action中自动拷贝

 

    //获取从页面表单中提交过来的值

       UserActionForm uaf = (UserActionForm)form;

User user = new User();

       //名称一样类型一样才行

       BeanUtils.copyProperties(user, uaf);

       user.setCreateDate(new Date());

      

       //调用业务逻辑操作

       UserManager.getInstance().addUser(user);

 

 

14、动态actionFrom

 

1、动态验证框架:

先在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>

 

查看validation.xml的配置

 

2、动态actionFrom

 

struts-config.xml

 

* struts-config.xml文件中定义动态ActionForm,如:

    <form-beans>

    //struts包的action中的动态actionFrom中获取

       <form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm">

           <form-property name="username" type="java.lang.String" />

           <form-property name="age" type="java.lang.Integer"/>

       </form-bean>

    </form-beans>

可以结合动态验证框架

 

    *Action中使用动态ActionForm,参见DynaActionFormTestAction.java

       DynaActionForm daf = (DynaActionForm)form;

       String username = (String)daf.get("username");

       Integer age = (Integer)daf.get("age");

 

jsp页面中

 

<body>

    用户名称:${dynaForm.map.username }<br>

    年龄:${dynaForm.map.age }<br>

</body>

 

14Struts文件上传

 

Jsp页面设置

 

    <li>测试struts上传文件</li><br>

    <form action="upload.do" method="post" enctype="multipart/form-data">

        标题:<input type="text" name="title"><br>

        文件:<input type="file" name="myfile"><br>

        <input type="submit" value="提交">

    </form>

 

配置struts-config.xml

<form-beanname="uploadForm"type="com.bjsxt.struts.UploadActionForm"/>

 

配置文件缓冲等属性可以看

配置struts-config.xml

<controller maxFileSize="10M"/>

查看Struts.jar里面的resources文件的struts-config1.2.jar内容

 

配置from表达收集

 

private String title;

   

    //必须采用FormFile

    private FormFile myfile;

 

配置action

 

UploadActionForm uaf = (UploadActionForm)form;

       System.out.println("title" + uaf.getTitle());

       FormFile myFile = uaf.getMyfile();

       if (myFile != null) {

           System.out.println("fileName=" + myFile.getFileName());

          

           FileOutputStream fos = new FileOutputStream("c://" + myFile.getFileName());

           fos.write(myFile.getFileData());

           fos.flush();

           fos.close();

       }

       return mapping.findForward("success");

 

配置跳转页面

 

<body>

    标题:${uploadForm.title}<br>

    文件名:${uploadForm.myfile.fileName  }<br>

</body>

 

16空字段测试

    * 在表单中没有input输入域,jsp脚本接收到的值为nullel表达式接收到的值为空串

    * 如果表单中的值,没有输入,那么jsp脚本和el表达式接收到的值都为空串

 

17、字符串转换器

servlet转换

Date类型的转换:

        * 如果是java.sql.Date,页面日期的格式必须为yyyy-mm-dd,才可以转换

       * 如果是java.util.Date,默认情况下struts无法转换

 

* 自定义转换器的实现步骤

       * 实现converter接口,实现convert方法

       * 将实现的conerter注册,通常情况采用servlet注册

       * 采用servlet注册需要注意标签的配置,<load-on-startup>10</load-on-startup>(也可以采用struts plugin注册)

 

Jsp页面

    <li>测试ActionForm类型转换器</li><br>

java.sql.Date:<input type="text" name="sqlDate"><br>

        java.util.Date:<input type="text" name="utilDate"><br>

 

actionfrom

private java.sql.Date sqlDate;

   

    private java.util.Date utilDate;

 

建立一个转换类

 

import java.util.Date;

 

* java.util.Date类型转换器

 * @author Administrator

 *

 */

public class UtilDateConverter implements Converter {

 

    public Object convert(Class type, Object value) {

       System.out.println("UtilDateConverter.value=" + value);

       if (value == null) {

           return value;

       }

       if (value instanceof Date) {

           return value;

       }

       Date d = null;

       if (value instanceof String) {

           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

           try {

              d = sdf.parse((String)value);

           } catch (ParseException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

       }

       return d;

    }

 

}

 

建立servlet转换utildate

 

public class UtilDateConverterInitWithServlet extends HttpServlet {

 

    @Override

    public void init() throws ServletException {

       System.out.println("UtilDateConverterInitWithServlet.init()");

       ConvertUtils.register(new UtilDateConverter(), Date.class);

    }

 

Jsp页面

 

java.sql.dateValue:${typeconvertForm.sqlDate }<br>

    java.util.utilValue:${typeconvertForm.utilDate }<br>

 

采用plugin方式集成

 

写完转换类侯写

public class UtilDateConverterInitWithPlugin implements PlugIn {

 

    public void destroy() {

    }

 

    public void init(ActionServlet servlet, ModuleConfig config)

           throws ServletException {

       System.out.println("UtilDateConverterInitWithPlugin.init()");

       ConvertUtils.register(new UtilDateConverter(), Date.class);

    }

}

 

struts-config.xml中设置

<plug-inclassName="com.bjsxt.struts.UtilDateConverterInitWithPlugin">

 

17、进入受保护页面的问题

 

登陆设置action

UserManager.getInstance().login(username, password);

           //request.setAttribute("username", username);

           request.getSession().setAttribute("user", username);

 

 

//一般session为空就直接重定向

设置struts-config配置文件的

<forward name="login" path="/login.jsp" redirect="true"/>

 

原创粉丝点击