<bdo id='6yyDr'></bdo><ul id='6yyDr'></ul>

      <tfoot id='6yyDr'></tfoot>
    1. <i id='6yyDr'><tr id='6yyDr'><dt id='6yyDr'><q id='6yyDr'><span id='6yyDr'><b id='6yyDr'><form id='6yyDr'><ins id='6yyDr'></ins><ul id='6yyDr'></ul><sub id='6yyDr'></sub></form><legend id='6yyDr'></legend><bdo id='6yyDr'><pre id='6yyDr'><center id='6yyDr'></center></pre></bdo></b><th id='6yyDr'></th></span></q></dt></tr></i><div id='6yyDr'><tfoot id='6yyDr'></tfoot><dl id='6yyDr'><fieldset id='6yyDr'></fieldset></dl></div>
      1. <small id='6yyDr'></small><noframes id='6yyDr'>

        <legend id='6yyDr'><style id='6yyDr'><dir id='6yyDr'><q id='6yyDr'></q></dir></style></legend>
      2. 如何从 jsp 将日期输入到 Struts2 动作?

        时间:2023-09-25
          <tbody id='9B3jc'></tbody>
        • <tfoot id='9B3jc'></tfoot>
        • <small id='9B3jc'></small><noframes id='9B3jc'>

          <i id='9B3jc'><tr id='9B3jc'><dt id='9B3jc'><q id='9B3jc'><span id='9B3jc'><b id='9B3jc'><form id='9B3jc'><ins id='9B3jc'></ins><ul id='9B3jc'></ul><sub id='9B3jc'></sub></form><legend id='9B3jc'></legend><bdo id='9B3jc'><pre id='9B3jc'><center id='9B3jc'></center></pre></bdo></b><th id='9B3jc'></th></span></q></dt></tr></i><div id='9B3jc'><tfoot id='9B3jc'></tfoot><dl id='9B3jc'><fieldset id='9B3jc'></fieldset></dl></div>

            1. <legend id='9B3jc'><style id='9B3jc'><dir id='9B3jc'><q id='9B3jc'></q></dir></style></legend>
                <bdo id='9B3jc'></bdo><ul id='9B3jc'></ul>
                • 本文介绍了如何从 jsp 将日期输入到 Struts2 动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的 jsp 页面中遇到了很多问题.我应该使用什么标签从用户那里获取日期(yyyy-MM-dd)并将其存储在 Struts2 操作的 Date 属性中?

                  I am facing a lot of problems here in my jsp page. What tag I should use to get a date (yyyy-MM-dd) from user and store it in a Date property of a Struts2 action ?

                  我的操作中的属性被声明为 java.util.Date.我希望来自 jsp 页面的输入登陆这个属性.

                  The property in my action is declared to be of java.util.Date. I want the input from jsp page to land in this property.

                  请帮忙.

                  如果使用 s:textfield 标记以正确的格式输入日期,我会收到 Invalid field 错误(在 JSP 中).

                  I get Invalid field error (in JSP) if is use a s:textfield tag to manyally enter a date in the right format.

                  推荐答案

                  我知道这篇文章有点老了,但一个解决方案可能对其他人有用.

                  I know this post is a bit old but a solution may be useful to others.

                  Struts 的默认转换器似乎无法正常工作.字段错误甚至发生在从操作填充的只读字段中.

                  The default converter of Struts does not seem to work properly. The field error even occurs with readonly fields populated from the action.

                  我通过定义自己的转换器解决了这个问题:

                  I resolved it by defining my own converter:

                  1. 创建转换器类(使用您需要的日期格式):

                  1. Create the converter class (using the date format you need):

                  public class StringToDateTimeConverter extends StrutsTypeConverter {
                      // WARNING not safe in multi-threaded environments
                      private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
                  
                      public Object convertFromString(Map context, String[] strings, Class toClass) {     
                          if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) {
                              return null;
                          }
                  
                          try {
                              return DATETIME_FORMAT.parse(strings[0]);
                          } catch (ParseException e) {
                              throw new TypeConversionException("Unable to convert given object to date: " + strings[0]);
                          }
                      }
                  
                      public String convertToString(Map context, Object date) {
                          if (date != null && date instanceof Date) {         
                              return DATETIME_FORMAT.format(date);
                          } else {
                              return null;
                          }
                      }
                  }
                  

                • 将转换注解放在属性设置器上(或使用conversion.properties 文件)

                • Put the conversion annotation on the property setter (or use a conversion.properties file)

                  @Conversion()
                  public class MyEditAction {
                      // ...
                  
                      @TypeConversion(converter="my.app.common.converter.StringToDateTimeConverter")
                      public void setUploadedDate(Date uploadedDate) {
                          this.uploadedDate = uploadedDate;
                      }
                  

                • 在我的情况下,我不需要编辑它,但想在 jsp 中指定格式.我使用了一个额外的隐藏字段来保留原始值(另一种选择是使用 Preparable 接口):

                  In my case I did not need to edit it but wanted to specify the format in the jsp. I used an additional hidden field to keep the original value (another alternative would have been the use of Preparable interface):

                  <s:textfield name="uploadedDateDisplay" value="%{getText('format.datetimesecond',{uploadedDate})}" size="70" disabled="true"/>
                  <s:hidden name="uploadedDate" />
                  

                  这篇关于如何从 jsp 将日期输入到 Struts2 动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Struts 2:Action之间的参数 下一篇:在 Struts 2 和 Struts 中使用 cookie

                  相关文章

                  1. <small id='bfmAw'></small><noframes id='bfmAw'>

                  2. <i id='bfmAw'><tr id='bfmAw'><dt id='bfmAw'><q id='bfmAw'><span id='bfmAw'><b id='bfmAw'><form id='bfmAw'><ins id='bfmAw'></ins><ul id='bfmAw'></ul><sub id='bfmAw'></sub></form><legend id='bfmAw'></legend><bdo id='bfmAw'><pre id='bfmAw'><center id='bfmAw'></center></pre></bdo></b><th id='bfmAw'></th></span></q></dt></tr></i><div id='bfmAw'><tfoot id='bfmAw'></tfoot><dl id='bfmAw'><fieldset id='bfmAw'></fieldset></dl></div>

                    • <bdo id='bfmAw'></bdo><ul id='bfmAw'></ul>

                    1. <tfoot id='bfmAw'></tfoot>
                      <legend id='bfmAw'><style id='bfmAw'><dir id='bfmAw'><q id='bfmAw'></q></dir></style></legend>