我想实现的效果是用户在登录界面输入用户名和密码,同时输入他的生日,如果用户名和密码正确(默认都是admin)就跳转到成功页面,并且输出用户名和他的生日时间,我想让它的输出生日时间是按照我定义的时间类型输出。我在struts2中定义一个格式转换类型,然后在配备properties。但输出的时间类型总是国际类型,不是我所定义的时间类型,这说明我定义的格式转换类型不起作用,大家帮我看看吧:
下面是我的格式转换类型的定义:
public class MyDateTypeConverter extends StrutsTypeConverter { public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try {
Date v = sdf.parse(arg1[0]);
return v;
} catch (ParseException e) { e.printStackTrace(); return new Date();
} } public String convertToString(Map arg0, Object arg1) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); Date v = (Date) arg1; return sdf.format(v);
}}下面是我的User定义主要是收集表单输入的数据:
public class User {


private String id;

private String name;

private String password;

private Date birthday;

public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
}}
下面是我的登录界面跳转的action
package com.struts2.demo.action;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.Action;public class LoginAction implements Action, ServletRequestAware {

private User user;

private HttpServletRequest request; public String execute() throws Exception {

if(user.getName()==null||user.getName().equals("")||user.getPassword()==null||user.getPassword().equals("")){
request.setAttribute("Msg", "密码或用户名不能为空");
return "error2";
}else if(user.getName().equals("admin")&&user.getPassword().equals("admin")){
request.getSession().setAttribute("user", user);
System.out.println(user);
return "success2";
}else{
request.setAttribute("Msg", "密码或者用户名不正确");
return "error2";
}

} public void setServletRequest(HttpServletRequest request) {
this.request=request; } public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}}
我的struts.xml配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>

<constant name="struts.devModel" value="true"/>

<package name="usermanager" extends="struts-default" namespace="/my">


<action name="loginAction" class="com.struts2.demo.action.LoginAction">
<result name="success2">/success2.jsp</result>
<result name="error2">/error2.jsp</result>
</action>

</package>
    
</struts>
下面是我验证成功后通跳转的页面success2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>first struts2demo</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>
  <h1>登录成功页面</h1>
    <p style="color: red"><s:property value="#session.user.name"/></p>
    <p style="color: blue">${user.birthday}</p>  </body>
</html>
下面是我的登录界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.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>
  
   <form action="my/loginAction" method="post">
    用户名:<input type="text" name="user.name" value=""><br/>
    用户密码:<input type="password" name="user.password" value=""><br/>
   出生年月日:<input type="text" name="user.birthday" value=""><br/>
   
    <input type="submit" value="确定">
   </form>
  </body>
</html>
下面是我的User-conversion.properties
birthday=com.struts2.demo.action.MyDateTypeConverter我的格式转换类和User-conversion.properties都是放在com.struts2.demo.action包下面的但是当我在登录页面输入2010-02-01的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了

解决方案 »

  1.   

    csdn的编辑框字体颜色右边有个插入源代码可以进行源码的格式化
    你这样的源码看着太乱了
      

  2.   

    这是不是譬如2011—2-2或者2011/2/2这样的格式阿
    你可以用标签库阿~我记得直接在标签里面加个format=“”。。貌似可以解决阿~~感觉你说的好麻烦阿
      

  3.   

    我想实现的效果是用户在登录界面输入用户名和密码,同时输入他的生日,如果用户名和密码正确(默认都是admin)就跳转到成功页面,并且输出用户名和他的生日时间,我想让它的输出生日时间是按照我定义的时间类型输出。我在struts2中定义一个格式转换类型,然后在配备properties。但输出的时间类型总是国际类型,不是我所定义的时间类型,这说明我定义的格式转换类型不起作用,大家帮我看看吧:
    下面是我的格式转换类型的定义:package com.struts2.demo.action;import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;public class MyDateTypeConverter extends StrutsTypeConverter { public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try {
    Date v = sdf.parse(arg1[0]);
    return v;
    } catch (ParseException e) { e.printStackTrace(); return new Date();
    } } public String convertToString(Map arg0, Object arg1) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); Date v = (Date) arg1; return sdf.format(v);
    }}
    下面是我的User定义主要是收集表单输入的数据:
    package com.struts2.demo.action;import java.util.Date;public class User {


    private String id;

    private String name;

    private String password;

    private Date birthday;

    public String getId() {
    return id;
    } public void setId(String id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } public Date getBirthday() {
    return birthday;
    } public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }}
    下面是我的登录界面跳转的action
    package com.struts2.demo.action;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.Action;public class LoginAction implements Action, ServletRequestAware {

    private User user;

    private HttpServletRequest request; public String execute() throws Exception {

    if(user.getName()==null||user.getName().equals("")||user.getPassword()==null||user.getPassword().equals("")){
    request.setAttribute("Msg", "密码或用户名不能为空");
    return "error2";
    }else if(user.getName().equals("admin")&&user.getPassword().equals("admin")){
    request.getSession().setAttribute("user", user);
    System.out.println(user);
    return "success2";
    }else{
    request.setAttribute("Msg", "密码或者用户名不正确");
    return "error2";
    }

    } public void setServletRequest(HttpServletRequest request) {
    this.request=request; } public User getUser() {
    return user;
    } public void setUser(User user) {
    this.user = user;
    }}
    我的struts.xml配置如下
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>

    <constant name="struts.devModel" value="true"/>

    <package name="usermanager" extends="struts-default" namespace="/my">


    <action name="loginAction" class="com.struts2.demo.action.LoginAction">
    <result name="success2">/success2.jsp</result>
    <result name="error2">/error2.jsp</result>
    </action>

    </package>
        
    </struts>
    下面是我验证成功后通跳转的页面success2.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <base href="<%=basePath%>">
        
      <title>first struts2demo</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>
      <h1>登录成功页面</h1>
      <p style="color: red"><s:property value="#session.user.name"/></p>
      <p style="color: blue">${user.birthday}</p>  </body>
    </html>
    下面是我的登录界面
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'login.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>
      
       <form action="my/loginAction" method="post">
        <input type="text" name="user.name" value=""><br/>
        <input type="password" name="user.password" value=""><br/>
        <input type="text" name="user.birthday" value=""><br/>
       
        <input type="submit" value="确定">
       </form>
      </body>
    </html>
    下面是我的User-conversion.properties
    birthday=com.struts2.demo.action.MyDateTypeConverter
    我的格式转换类和User-conversion.properties都是放在com.struts2.demo.action包下面的但是当我在登录页面输入2010-02-01的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了
      

  4.   

    我想实现的效果是用户在登录界面输入用户名和密码,同时输入他的生日,如果用户名和密码正确(默认都是admin)就跳转到成功页面,并且输出用户名和他的生日时间,我想让它的输出生日时间是按照我定义的时间类型输出。我在struts2中定义一个格式转换类型,然后在配备properties。但输出的时间类型总是国际类型,不是我所定义的时间类型,这说明我定义的格式转换类型不起作用,大家帮我看看吧:
    下面是我的格式转换类型的定义:
    package com.struts2.demo.action;import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;public class MyDateTypeConverter extends StrutsTypeConverter {public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");try {
    Date v = sdf.parse(arg1[0]);
    return v;
    } catch (ParseException e) {e.printStackTrace();return new Date();
    }}public String convertToString(Map arg0, Object arg1) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");Date v = (Date) arg1;return sdf.format(v);
    }}下面是我的User定义主要是收集表单输入的数据:
    package com.struts2.demo.action;import java.util.Date;public class User {
    private String id;private String name;private String password;private Date birthday;public String getId() {
    return id;
    }public void setId(String id) {
    this.id = id;
    }public String getName() {
    return name;
    }public void setName(String name) {
    this.name = name;
    }public String getPassword() {
    return password;
    }public void setPassword(String password) {
    this.password = password;
    }public Date getBirthday() {
    return birthday;
    }public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }}下面是我的登录界面跳转的action
    package com.struts2.demo.action;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.Action;public class LoginAction implements Action, ServletRequestAware {private User user;private HttpServletRequest request;public String execute() throws Exception {if(user.getName()==null||user.getName().equals("")||user.getPassword()==null||user.getPassword().equals("")){
    request.setAttribute("Msg", "密码或用户名不能为空");
    return "error2";
    }else if(user.getName().equals("admin")&&user.getPassword().equals("admin")){
    request.getSession().setAttribute("user", user);
    System.out.println(user);
    return "success2";
    }else{
    request.setAttribute("Msg", "密码或者用户名不正确");
    return "error2";
    }}public void setServletRequest(HttpServletRequest request) {
    this.request=request;}public User getUser() {
    return user;
    }public void setUser(User user) {
    this.user = user;
    }}我的struts.xml配置如下
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devModel" value="true"/><package name="usermanager" extends="struts-default" namespace="/my">
    <action name="loginAction" class="com.struts2.demo.action.LoginAction">
    <result name="success2">/success2.jsp</result>
    <result name="error2">/error2.jsp</result> 
    </action></package>
        
    </struts>下面是我验证成功后通跳转的页面success2.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <base href="<%=basePath%>">
        
      <title>first struts2demo</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>
      <h1>登录成功页面</h1>
      <p style="color: red"><s:property value="#session.user.name"/></p>
      <p style="color: blue">${user.birthday}</p>  </body>
    </html>下面是我的登录界面
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <base href="<%=basePath%>">
        
      <title>My JSP 'login.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>
       
      <form action="my/loginAction" method="post">
      <input type="text" name="user.name" value=""><br/>
      <input type="password" name="user.password" value=""><br/>
      <input type="text" name="user.birthday" value=""><br/>
      
      <input type="submit" value="确定">
      </form>
      </body>
    </html>下面是我的User-conversion.properties
    birthday=com.struts2.demo.action.MyDateTypeConverter
    我的格式转换类和User-conversion.properties都是放在com.struts2.demo.action包下面的但是当我在登录页面输入2010-02-01的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了
    这下应该好看点了吧,呵呵,再次辛苦大家了啊
      

  5.   

    Struts2 默认的是将表单数据转化为String类型的!
      

  6.   

    下面是我的User-conversion.properties应该是LoginAction-conversion.properties
    然后birthday=com.struts2.demo.action.MyDateTypeConverter应该是user.birthday=com.struts2.demo.action.MyDateTypeConverter试试行不不行再说
      

  7.   

    JayYounger我刚刚试过了,还是一样的哦,没有反应的啊。
      

  8.   

    //Date v = (Date) arg1;
    这句话可以不要
    直接return sdf.format(arg1[0]);
    //试试吧
      

  9.   


    找到一种办法,分享一下
    定义xwork-conversion.properties,放在src目录下。
    内容为:
    java.util.Date=com.struts2.demo.action.MyDateTypeConverter在jsp页面用ognl标签取值。