当前位置:编程学习 > JS >>

Struts2 学习笔记19 类型转换 Part1

 现在来说一说类型转换,提到类型转换其实我们之前早已经用过了,在url传递参数的时候,我们传递过来的参数其实都是String类型的,在显示的时候都自动转换了,像这种简单的转换很好理解,我们要说的是,转换为那些相对不简单的类型。首先我们建立一个小项目。
 
TestAction.java
 
<span style="font-size:18px"><strong>package com.tfj.action;  
  
import java.util.Date;  
import java.util.List;  
import java.util.Map;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class TestAction extends ActionSupport{  
    private int age;  
    private String name;  
    private Date d;  
    List<String> interests;  
    Map<String,String> user;  
      
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public Date getD() {  
        return d;  
    }  
  
    public void setD(Date d) {  
        this.d = d;  
    }  
  
    public List<String> getInterests() {  
        return interests;  
    }  
  
    public void setInterests(List<String> interests) {  
        this.interests = interests;  
    }  
  
    public Map<String, String> getUser() {  
        return user;  
    }  
  
    public void setUser(Map<String, String> user) {  
        this.user = user;  
    }  
  
    @Override  
    public String execute() throws Exception {  
          
        return SUCCESS;  
    }  
      
}</strong></span><em>  
</em>  
index.jsp

 

 
<span style="font-size:18px"><%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> 
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<%@taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="<%=basePath%>">  
      
    <title>My JSP 'index.jsp' starting page</title>  
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">  
    <!-- 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    -->  
  </head>  
    
  <body>  
   age:<s:property value="age"/><br/>  
   name:<s:property value="name"/><br/>  
   date:<s:property value="d"/><br/>  
   <s:date name="d" format="yyyy/MM/dd HH:mm:ss"/><br/>  
   interests:<s:property value="interests"/><br/>  
   user:<s:property value="user"/><br/>  
  </body>  
</html></span>  

 

  对于age和name变量,在参数传递的部分已经说过了,只要注意是这里的struts2完成了一个转换。
补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,