我想实现的效果是用户在登录界面输入用户名和密码,同时输入他的生日,如果用户名和密码正确(默认都是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的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了
下面是我的格式转换类型的定义:
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的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了
解决方案 »
- myeclipse采用xfire根据wsdl生成本地文件报错
- struts1.3+hibernate遇到的错误.
- 关系查询时,保存查询数值的问题!!!
- 郁闷啊!!面试题中遇到的!!
- Oscache设置JMS集群启动weblogic报错
- java程序如何调用迅雷下载工具
- 启动TOMCAT出现 报错:in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.5.0_08\bin;C:\Tomcat
- 用hibernate生成时候的错误,请指点。
- java的一个问题,请大家帮下我。
- Jboss中如何修改服务端口?我想改成8098端口....
- 关于如何提高ssh框架的运行速度??
- 正则表达式替换html标签?
你这样的源码看着太乱了
你可以用标签库阿~我记得直接在标签里面加个format=“”。。貌似可以解决阿~~感觉你说的好麻烦阿
下面是我的格式转换类型的定义: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的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了
下面是我的格式转换类型的定义:
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的时候,它的输出页面老是输出国际时间,这是怎么回事,这个要怎么改呢,大家帮帮忙哦先谢谢大家了
这下应该好看点了吧,呵呵,再次辛苦大家了啊
然后birthday=com.struts2.demo.action.MyDateTypeConverter应该是user.birthday=com.struts2.demo.action.MyDateTypeConverter试试行不不行再说
这句话可以不要
直接return sdf.format(arg1[0]);
//试试吧
找到一种办法,分享一下
定义xwork-conversion.properties,放在src目录下。
内容为:
java.util.Date=com.struts2.demo.action.MyDateTypeConverter在jsp页面用ognl标签取值。