struts2 内置的类型转换(10)

类型转换

例子介绍:
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>
可以看到如下效果:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值