为什么我定义的类型转换不起作用呢?
我想实现的效果是用户在登录界面输入用户名和密码,同时输入他的生日,如果用户名和密码正确(默认都是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"%>