在实际开发中,覆盖默认错误信息以及自定义类型转换器还是很有必要的的。毕struts是外国人开发的,默认的一些东西都不太合我们的胃口
首先放上的index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri = "/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.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>
<!--
怎么覆盖struts定义的默认错误信息:
1)在对应的Action包中新建一个.properties文件,文件名为对应的ActionName
2)在属性文件中添加如下键值对:invalid.fieldvalue.filedName=xxx //xxx代表着你自己定义的错误信息
如果是simple主题的话,不会自动显示错误信息,让其显示的方法为:
1)通过debug标签可知,若转换出错,在值栈栈顶的Action(必须实现ValidationAware接口)可以找到错误信息,
通过:${fieldErrors.fieldName[index]}获取错误信息
2)通过<s:fielderror name="fieldName"/>
如果是simple主题的话,怎么自定义<s:fielderror name="fieldName"/>在html中的格式
在org.apache.struts2/template/simple下有一个fielderror.ftl文件,
定义了在simple主题下的s:fielderror的样式,在src下建一个template/simple包再新建一个.ftl文件复制struts2下的
fielderror.ftl文件内容到自定义的fielderror.ftl文件中,再修改即可,比如去除ul li span标签
如何自定义类型转换器。
1)局部类型转换器(基于字段的配置)
首先在model(有可能是action 或者是javebean 取决于属性所在的类中)所在的包中建一个ModelName-conversion.properties,
内容为:属性=类转换器的全称,然后新建一个类实现StrutsTypeConverter ,或者继承DefaultTypeConverter 。
第一次使用时创建对象,自始至终都只有一个
2)全局类型转换器(基于类型的配置)
首先在src目录下新建一个xwork-conversion.properties 内容为:待转类型=类转换器的全称
在当前struts2应用被加载时创建实例
当在web.xml里配置了初始化参数,在类型转换器里需要获取这个参数时,由于加载应用时没有创建ServletContext ,
ServletActionContext.getServletContext()会返回null,所以此时不能采用全局类型转换器这种配置方法
-->
<body>
<s:debug />
<s:form action = "conversion" theme = "simple">
<s:textfield label="Age:" name = "age" />
<s:fielderror fieldName = "age" ></s:fielderror>
<br> <br>
<s:textfield label = "Birth:" name = "birth" />
<s:fielderror fieldName="birth" /><br><br>
<s:submit value="提交"/>
</s:form>
</body>
</html>
以及ConvsersionAction.java
package cn.Lionel.web.demo1;
import cn.Lionel.web.demo1.model.Customer;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class ConversionAction extends ActionSupport implements ModelDriven<Customer>{
public Customer customer ;
public String execute(){
System.out.println("age:"+customer.getAge());
System.out.println("birth:"+customer.getBirth());
return "success";
}
@Override
public Customer getModel() {
return customer = new Customer();
}
}
Customer.java
package cn.Lionel.web.demo1.model;
import java.util.Date;
public class Customer {
private int age ;
private Date birth ;
/**
* @return the birth
*/
public Date getBirth() {
return birth;
}
/**
* @param birth the birth to set
*/
public void setBirth(Date birth) {
this.birth = birth;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
DateConvert.java
package cn.Lionel.web.demo1;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.StrutsTypeConverter;
public class DateConverter extends StrutsTypeConverter{
private DateFormat dateFormat ;
public DateConverter(){
System.out.println("DateConverter's constructor ........");
}
private void initDateFormat(){
if(dateFormat == null){
ServletContext context = ServletActionContext.getServletContext();
String pattern = context.getInitParameter("pattern");
this.dateFormat =new SimpleDateFormat(pattern) ;
}
}
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
System.out.println("convertFromString.........");
if(toClass == Date.class){
try {
initDateFormat() ;
return dateFormat.parseObject(values[0]);
} catch (ParseException e) {
throw new RuntimeException() ;
}
}
return values;
}
@Override
public String convertToString(Map context, Object o) {
System.out.println("convertToString...........");
if(o instanceof Date){
return dateFormat.format((Date)o);
}
return null ;
}
}
1:覆盖默认错误信息
1)在对应的Action包中新建一个.properties文件,文件名为对应的ActionName
由于我Action类是ConvsersionAction,所以我建得文件为:ConvsersionAction.properties
2)在属性文件中添加如下键值对:invalid.fieldvalue.filedName=xxx //xxx代表着你自己定义的错误信息
invalid.fieldvalue.age=\u60A8\u8F93\u5165\u7684age\u683C\u5F0F\u4E0D\u6B63\u786E\uFF01(文件内容)
如果是simple主题的话,不会自动显示错误信息,让其显示的方法为:
1)通过debug标签可知,若转换出错,在值栈栈顶的Action(必须实现ValidationAware接口)可以找到错误信息,
通过:${fieldErrors.fieldName[index]}获取错误信息
2)通过<s:fielderror name="fieldName"/>
如果是simple主题的话,怎么自定义<s:fielderror name="fieldName"/>在html中的格式
在org.apache.struts2/template/simple下有一个fielderror.ftl文件,
定义了在simple主题下的s:fielderror的样式,在src下建一个template/simple包再新建一个.ftl文件复制struts2下的
fielderror.ftl文件内容到自定义的fielderror.ftl文件中,再
以上就是覆盖错误信息的方法
2:自定义类型转换器
1)局部类型转换器(基于字段的配置)
首先在model(有可能是action 或者是javebean 取决于属性所在的类中)所在的包中建一个ModelName-conversion.properties,
内容为:属性=类转换器的全称,然后新建一个类实现StrutsTypeConverter ,或者继承DefaultTypeConverter 。
第一次使用时创建对象,自始至终都只有一个,由于我的Model是Customer,所以我建了一个Customer-conversion.properties
Customer-conversion.properties
birth =cn.Lionel.web.DateConverter
启动tomcat就可以运行了
2)全局类型转换器(基于类型的配置)
首先在src目录下新建一个xwork-conversion.properties 内容为:待转类型=类转换器的全称
在当前struts2应用被加载时创建实例
xwork-conversion.properties:
java.util.Date =cn.Lionel.web.demo1.DateConverter
启动tomcat就可以运行了