为什么我定义的类型转换不起作用呢?
我想实现的效果是用户在登录界面输入用户名和密码,同时输入他的生日,如果用户名和密码正确(默认都是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的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了
[解决办法]
下面是我的User-conversion.properties应该是LoginAction-conversion.properties
然后birthday=com.struts2.demo.action.MyDateTypeConverter应该是
user.birthday=com.struts2.demo.action.MyDateTypeConverter