zoukankan      html  css  js  c++  java
  • struts2-14-用户自定义全局转换器

     全局和局部的区别:

    1:配置文件不同  

               全局:src---xwork-conversion.properties---address=nuc.sw.action.AddressConverter

               局部:action---actionName-conversion.properties---nuc.sw.vo.Address=nuc.sw.action.AddressConverter

    2:显示内容时全局写一个address即可,局部需要写address.province。

    一:用户注册信息页面(地址为级联)  UserDetails.jsp

        

    复制代码
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib prefix="s" uri="/struts-tags"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>注册页面</title>
     9 </head>
    10 <body>
    11 <s:fielderror></s:fielderror>
    12     <form action="UserDetailsAction" method="post">
    13         用户名:<input type="text" name="username" /><br> 
    14         生日:<input type="text" name="birthday" />(格式为:yyyy-mm-dd)<br> 
    15         详细地址:<input type="text" name="address">省&nbsp;&nbsp;
    16                <input type="text" name="address">市&nbsp;&nbsp;
    17                <input type="text" name="address">区<br>
    18                <input type="text" name="address"/>        
    19         <input type="submit" value="注册">
    20     </form>
    21 </body>
    22 </html>
    复制代码

    二:在action写set get方法 nuc.sw.action--->UserDetailsAction.java

      

    复制代码
     1 package nuc.sw.action;
     2 import java.util.Date;
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 import nuc.sw.vo.Address;
     6 public class UserDetailsAction extends ActionSupport{
     7        private String username;
     8        //注意类型
     9        private Date birthday;
    10        //必须为Address类型。否则获取不到。
    11        private Address address;
    12     public Date getBirthday() {
    13         return birthday;
    14     }
    15     public void setBirthday(Date birthday) {
    16         this.birthday = birthday;
    17     }
    18     public String getUsername() {
    19         return username;
    20     }
    21     public void setUsername(String username) {
    22         this.username = username;
    23     }
    24 
    25 
    26 
    27     public Address getAddress() {
    28         return address;
    29     }
    30 
    31 
    32     public void setAddress(Address address) {
    33         this.address = address;
    34     }
    35 
    36 
    37     public String RegisterMethod() throws Exception {
    38         return SUCCESS;
    39    }
    40         
    41 }
    复制代码

    三:获取Address类型  所以写Address.java --->nuc.sw.vo

    复制代码
    package nuc.sw.vo;
    
    public class Address {
    
        private String province;
        private String city;
        private String county;
        private String street;
        public Address(String province, String city, String county, String street) {
            super();
            this.province = province;
            this.city = city;
            this.county = county;
            this.street = street;
        }
    
        public String getProvince() {
            return province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getCounty() {
            return county;
        }
    
        public void setCounty(String county) {
            this.county = county;
        }
    
        public String getStreet() {
            return street;
        }
    
        public void setStreet(String street) {
            this.street = street;
        }
    
    }
    复制代码

    四:配置struts.xml

    复制代码
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7  <constant name="struts.devMode" value="true" />
     8  <package name="default" namespace="/" extends="struts-default">
     9      <action name="UserDetailsAction" class="nuc.sw.action.UserDetailsAction" method="RegisterMethod">
    10          <result>/showUserDetails.jsp</result>
    11          <result name="input">/UserDetails.jsp</result>
    12      </action>
    13  </package>
    14 </struts>
    复制代码

    五:用户提交信息后进行显示  showUserDetails.jsp

    生日的月份需要MM---mm指时间

    复制代码
     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <%@ taglib prefix="s" uri="/struts-tags"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     8 <title>注册成功后登录</title>
     9 </head>
    10 <body>
    11 用户名:<s:property value="username"/><br>
    12 生日:   <s:date name="birthday" format="yyyy-MM-dd"/><br>
    13 您的详细地址为:<br>
    14     <s:property value="address"/>
    15    
    16     
    17     
    18 </body>
    19 </html>
    复制代码

    六:提交的地址信息为字符串String,最后要以对象的形式输出,所以需要自定义转换器

         nuc.sw.action--->AddressConverter.java

    复制代码
     1 package nuc.sw.action;
     2 
     3 import java.util.Map;
     4 
     5 import org.apache.struts2.util.StrutsTypeConverter;
     6 import nuc.sw.vo.Address;
     7 public class AddressConverter extends StrutsTypeConverter {
     8 
     9     /*
    10      *1:转换器必须继承StrutsTypeConverter
    11      *2:自带方法  字符串转为value(address)
    12      *3:自带方法  value转为字符串 
    13      */
    14     @Override
    15     public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
    16         
    17         //此处可以简略代码,但是必须在Address.java写构造方法
    18         Address address=new Address(arg1[0],arg1[1],arg1[2],arg1[3]);
    19 
    20         //String[]中放Address中的4个值
    21 //        address.setProvince(arg1[0]);
    22 //        address.setCity(arg1[1]);
    23 //        address.setCounty(arg1[2]);
    24 //        address.setStreet(arg1[3]);
    25         return address;
    26     }
    27 
    28     
    29     @Override
    30     public String convertToString(Map arg0, Object arg1) {
    31         //ag1是父类,Address是子类,需要强制转换。
    32         if(arg1 instanceof Address){
    33             Address a=(Address)arg1;
    34             return a.getProvince()+"省"+a.getCity()+"市"+a.getCounty()+"区"+a.getStreet();
    35         }
    36         else
    37           return null;
    38     }
    39 
    40 }
    复制代码

    七:局部转换器 ---写src中属性名建的配置文件

     1 nuc.sw.vo.Address=nuc.sw.action.AddressConverter 

    八:项目结构:

        

    九:运行结果:

     

  • 相关阅读:
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】不为人知的稠密特征加入CTR预估模型的方法
    205. 判断两个字符串的模式是否相同 Isomorphic Strings
    541. 反转字符串2 Reverse String II
    插入排序,二分查找插入排序,使用二叉树的插入排序
    二分查找,求mid值的类型溢出问题
    二叉搜索树类的C#实现
    二叉搜索树,删除节点
    二叉搜索树的前驱节点和后继节点
    438. 匹配字符串(顺序不同但个数相同的字符串) Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/Z-D-/p/6007857.html
Copyright © 2011-2022 走看看