关于struts1转换器和插件
<controller processorClass="controller.CharacterController"></controller>
<message-resources parameter="MessageResources" />
<plug-in className="plugin.DateConvertPlugIn"></plug-in>
转换器:
1,关于控制器的配置必须写在<message-resources>的后面,否则会报错!
2,
public class CharacterController extends RequestProcessor {
@Override
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("struts 控制器已经加入");
try {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.processPreprocess(request, response);
}
}
插件:
1,
public class UtilDateConvter implements Converter {
public Object convert(Class arg0, Object text) {
// TODO Auto-generated method stub
Date date=null;
if(text==null)
return (null);
if(text instanceof Date)
return text;
if(text instanceof String)
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
try {
date= sdf.parse((String)text);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return date;
}
}
2,
public class DateConvertPlugIn implements PlugIn {
public void destroy() {
// TODO Auto-generated method stub
}
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
System.out.println("日期转换器开始工作。。。");
ConvertUtils.register(new UtilDateConvter(), java.util.Date.class);
}
}
国际化:
<body>
<html:errors/>
<form action="users.do" method="post" name="myform">
用户ID:<input type="text" name="users.uid"/><br/>
<bean:message key="login.uname"/><input type="text" name="users.uname"/><br/>
<bean:message key="login.upass"/><input type="text" name="users.upass"/><html:errors property="password_error"/><br/>
登录时间:<input type="text" name="users.loginTime" /><br/>
<input type="hidden" name="method" value="dologin"/>
<input type="submit" value='<bean:message key="login.submit"/>'/>
</form>
</body>
<controller processorClass="controller.CharacterController"></controller>
<message-resources parameter="MessageResources" />
<plug-in className="plugin.DateConvertPlugIn"></plug-in>
转换器:
1,关于控制器的配置必须写在<message-resources>的后面,否则会报错!
2,
public class CharacterController extends RequestProcessor {
@Override
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("struts 控制器已经加入");
try {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.processPreprocess(request, response);
}
}
插件:
1,
public class UtilDateConvter implements Converter {
public Object convert(Class arg0, Object text) {
// TODO Auto-generated method stub
Date date=null;
if(text==null)
return (null);
if(text instanceof Date)
return text;
if(text instanceof String)
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
try {
date= sdf.parse((String)text);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return date;
}
}
2,
public class DateConvertPlugIn implements PlugIn {
public void destroy() {
// TODO Auto-generated method stub
}
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
System.out.println("日期转换器开始工作。。。");
ConvertUtils.register(new UtilDateConvter(), java.util.Date.class);
}
}
国际化:
<body>
<html:errors/>
<form action="users.do" method="post" name="myform">
用户ID:<input type="text" name="users.uid"/><br/>
<bean:message key="login.uname"/><input type="text" name="users.uname"/><br/>
<bean:message key="login.upass"/><input type="text" name="users.upass"/><html:errors property="password_error"/><br/>
登录时间:<input type="text" name="users.loginTime" /><br/>
<input type="hidden" name="method" value="dologin"/>
<input type="submit" value='<bean:message key="login.submit"/>'/>
</form>
</body>