zoukankan      html  css  js  c++  java
  • struts2中类型转换器

    Struts2中的数据类型转换以及自定义数据类型转换器,数据类型转换错误的错误流输出

    局部类型转换器注册,全局类型转换器注册,数据校验

    自定义类型转换器:

    package com.inspur.util;

    import java.util.ArrayList;

    import java.util.List;

    import java.util.Map;

    import com.inspur.po.User;

    import org.apache.struts2.util.StrutsTypeConverter;

    public class UserConverter extends StrutsTypeConverter{

         @Override

         public Object convertFromString(Map context, String[] values, Class toClass) {

                  if(values.length>1){

                           List<User> userList=new ArrayList<User>();

                           for(int i=0;i<values.length;i++){

                                     User user=new User();

                                     String[] userValues=values[i].split(",");

                                     user.setUsername(userValues[0]);

                                     user.setPassword(userValues[1]);

                                     userList.add(user);

                                    

                           }

                           return userList;

                  }else{

                           User user=new User();

                           String[]userValues=values[0].split(",");

                           user.setUsername(userValues[0]);

                           user.setPassword(userValues[1]);

                           return user;

                  }

                 

         }

         @Override

         public String convertToString(Map context, Object object) {

                  String result="";

                  if(object instanceof User){

                           User user=(User)object;

                           result+="<"+user.getUsername()+","+user.getPassword()+">";

                          

                          

                  }else if(object instanceof List){

                          

                           List<User>userList=(List<User>)object;

                           result+="[";

                           for(User iterator_user:userList){

                                     result+="<"+iterator_user.getUsername()+","+iterator_user.getPassword()+">";

                                    

                           }

                           result+="]";

                  }

                  return result;

                 

         }

    }

    Action代码:

    package com.inspur.action;

    import java.util.Date;

    import com.inspur.po.User;

    import com.opensymphony.xwork2.ActionSupport;

    public class LoginAction extends  ActionSupport {

         private Date loginTime;

        

         public Date getLoginTime() {

                  return loginTime;

         }

         public void setLoginTime(Date loginTime) {

                  this.loginTime = loginTime;

         }

         private User user;

         private String tip;

        

         public User getUser() {

                  return user;

         }

         public void setUser(User user) {

                  this.user = user;

         }

         public String getTip() {

                  return tip;

         }

         public void setTip(String tip) {

                  this.tip = tip;

         }

         public String login(){

                  if(this.getUser().getUsername().equals("lzhq")&&this.getUser().getPassword().equals("123")){

                          

                           this.setTip("login success");

                           return SUCCESS;

                  }else

                           return ERROR;

                 

                 

         }

        

        

    }

    Jsp页面代码

    <form action="login.action" method="post" name="loginForm">

            name,pass:<input type="text" name="user"/><br/>

            loginTime:<input type="text" name="loginTime"/><br/>

            <input type="submit" value="login"/>

        </form>

      <s:fielderror label="converter error information"></s:fielderror>

  • 相关阅读:
    Linux中find命令用法全汇总,看完就没有不会用的!
    ubuntu16.04 通过命令,修改屏幕分辨率
    Linux下如何查看哪些进程占用的CPU内存资源最多
    shell脚本 在后台执行de 命令 >> 文件 2>&1 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)
    ef linq 访问视图返回结果重复
    asp.net core web 本地iis开发
    jQuery控制TR显示隐藏
    mvc EF 从数据库更新实体,添加视图实体时添加不上的问题
    无法确定依赖操作的有效顺序。由于外键约束、模型要求或存储生成的值,因此可能存在依赖关系
    还原差异备份——因为没有文件可用于前滚
  • 原文地址:https://www.cnblogs.com/moonfans/p/3024012.html
Copyright © 2011-2022 走看看