struts2 内置的类型转换(10)

来源:互联网 发布:php订货系统源码 编辑:程序博客网 时间:2024/06/05 06:38

类型转换

例子介绍:
test,jsp:
<%--  Created by IntelliJ IDEA.  User: Alex  Date: 2017/5/24  Time: 23:07  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="s" uri="/struts-tags" %><html>  <head>    <title>类型转换测试</title>  </head>  <body>  name:<s:property value="name" /> <br/>  age: <s:property value="age" />  </body></html>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"        "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="test" namespace="/" extends="struts-default">        <action name="test" class="com.alex.action.testAction">            <result>/test.jsp</result>        </action>    </package></struts>
testAction.java:
package com.alex.action;import com.opensymphony.xwork2.ActionSupport;/** * Created by Alex on 2017/5/24. */public class testAction extends ActionSupport {    private String name;    private int age;    @Override    public String execute() throws Exception{        return super.execute();    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}
非常简单的一个环境,运行后是这样子的:

在此时已经发生类型转换了.所谓类型转换就是把字符串与特定类型相互转换,
接下来我们在action里添加一个日期的变量:
package com.alex.action;import com.opensymphony.xwork2.ActionSupport;import java.util.Date;/** * Created by Alex on 2017/5/24. */public class testAction extends ActionSupport {    private String name;    private int age;    private Date d;    public Date getD() {        return d;    }    public void setD(Date d) {        this.d = d;    }    @Override    public String execute() throws Exception{        return super.execute();    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}
在前端使用<s:date>标签对日期类型进行转换,
<%--  Created by IntelliJ IDEA.  User: Alex  Date: 2017/5/24  Time: 23:07  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="s" uri="/struts-tags" %><html>  <head>    <title>类型转换测试</title>  </head>  <body>  name:<s:property value="name" /> <br/>  age: <s:property value="age" /><br/>  date: <s:property value="d"/><br/>  <s:date format="yyyy/MM/dd HH:mm:ss" name="d" />  </body></html>
可以看到如下效果:
原创粉丝点击