Struts2内建类型转换器数据器使用简介

struts2 内建了类型转换器,可以方便的将我们在页面上输入的字符串数据转换成JavaBean中的Boolean,Float,Integer,Double,Long等数据类型,在无法转换成功时,还可以给出错误提示,非常方便

首先简历JavaBean:

 

package  HelloWorld;

public   class  User  ... {
    
private String username;
      
private Integer password;

      
private String[] books;
      
public String[] getBooks() ...{
            
return books;
        }

        
public void setBooks(String[] books) ...{
            
this.books = books;
        }

        
public String getUsername() ...{
            
return username;
        }

        
public void setUsername(String username) ...{
            
this.username = username;
        }

        
public Integer getPassword() ...{
            
return password;
        }

        
public void setPassword(Integer password) ...{
            
this.password = password;
        }


    
}

 

编写Action,需要继承ActionSupport,在action中有两个变量,一个是User类,一个是birth属性

我们定义birth属性和user类中的password属性都必须是Integer类型

 

package  HelloWorld;

import  com.opensymphony.xwork2.ActionSupport;

public   class  LoginAction  extends  ActionSupport ... {
 

private String tip;
private User user;
private Integer birth;


public Integer getBirth() ...{
    
return birth;
}


public void setBirth(Integer birth) ...{
    
this.birth = birth;
}


public String execute() throws Exception...{
    System.out.println(
this.getUser().getUsername());
    System.out.println(
this.getUser().getPassword());
          
return SUCCESS;
    
  }


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;
}



  
}

 

web.xml

 

<? xml version="1.0" encoding="UTF-8" ?>
< web-app  version ="2.5"  
    xmlns
="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    
< filter >
      
< filter-name > struts2 </ filter-name >
      
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
    
</ filter >
    
< filter-mapping >
      
< filter-name > struts2 </ filter-name >
      
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >


</ web-app >

 

struts.xml (WEB-INF/classes下)

 

<? xml version="1.0" encoding="GBK"  ?>
<! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>

< struts >
    
< package  name ="struts"  extends ="struts-default" >
       
< action  name ="Login"  class ="HelloWorld.LoginAction" >
         
< result  name ="input" > /helloworld/index.jsp </ result >
         
< result  name ="success" > /helloworld/welcome.jsp </ result >
         
         
< result  name ="error" > /helloworld/error.jsp </ result >
       
</ action >
    
</ package >
</ struts >

 

struts.properties(WEB-INF/classes下)

struts.custom.i18n.resources=messageResource

 

信息录入页面:

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title></title>
  </head>
  <body>     
   <div style="color:red">
    <s:fielderror />
</div>
     <s:form action="Login">
       <s:textfield name="user.username" key="username"></s:textfield>

       <s:textfield name="user.password" key="password"></s:textfield>
 <s:textfield name="birth" key="password"></s:textfield>
       <s:submit value="login"></s:submit>
    </s:form>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  </body>
</html>

信息展示页面:

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@ page isELIgnored="false" %>
   
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title></title>
  </head>
  <body>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
   <body>
        转换成功!<br>
  用户1的用户名为:<s:property value="user.username"/><br>
  用户1的密码为:<s:property value="user.password"/><br>
  
    </body>
</html>

 

 

我们在资源文件中加入:

xwork.default.invalid.fieldvalue={0}类型转换错误,运行index.jsp.,在三个输入框中都输入admin,则会出现以下提示:


user.password字段类型转换失败
birth字段类型转换失败

 

在资源文件中定义的是全局转换错误提示的内容,如果想对具体Action中的字段进行提示,则编写ActionName.properties,本文中对应的就是LoginAction.properties,放在和Action同一个目录下,内容格式如下:invalid.fieldvalue.birth (其中birth为action中属性名)

本文LoginAction.properties内容为:invalid.fieldvalue.birth=生日必须为数字

再次运行index.jsp.结果如下:

user.password字段类型转换失败
生日必须位数字

你可能感兴趣的:(jsp,Web,xml,struts,javaee)