struts2自定义类型转换,没走类型转换器。
这段代码最后好像是没走自定义的类型转换器,我做的是局部转换器property文件也和Action在同一包内命名也是正确的,错误出在哪里呢?Action代码:publicc...
这段代码最后好像是没走自定义的类型转换器,我做的是局部转换器 property文件也和Action在同一包内 命名也是正确的,错误出在哪里呢?
Action代码:
public class TriangleArea extends ActionSupport implements ModelDriven<ThreeEdge>{
ThreeEdge threeedge = new ThreeEdge();
public ThreeEdge getThr() {
return threeedge;
}
public void setThr(ThreeEdge thr) {
this.threeedge = thr;
}
public String execute(){
threeedge.setS((threeedge.getA()+threeedge.getB()+threeedge.getC())/2.0);
threeedge.setArea((threeedge.getS() - threeedge.getA()) * (threeedge.getS() - threeedge.getB())*(threeedge.getS() - threeedge.getC()) * threeedge.getS());
System.out.println(threeedge.getArea());
return "success";
}
public ThreeEdge getModel() {
return null;
}}
封装
public class ThreeEdge{
private double a,b,c,area,s;
省略 get/set方法
}
自定义转换类
public class TriangleToConvert extends StrutsTypeConverter {
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
System.out.println("正在转换:从字符串转换到复合类型...");
ThreeEdge thr = new ThreeEdge();
String param = arg1[0];
String[] paramValues = param.split(",");
thr.setA(Double.parseDouble(paramValues[0]));
thr.setB(Double.parseDouble(paramValues[1]));
thr.setC(Double.parseDouble(paramValues[2]));
System.out.println(thr.getA());
return thr;}
public String convertToString(Map arg0, Object arg1) {
ThreeEdge thr = (ThreeEdge)arg0;
String result= "边1:" + thr.getA() + "边2:" + thr.getB() + "边3:" + thr.getC();
return result;
}}
文件
TriangleArea-conversion.properties里
threeedge=cn.edu.util.TriangleToConvert
jsp页面
<form id="form1" action="TriangleArea.action" name="form1" method="post">
<label>
三边:<input type="text" name="threeedge"/>
</label><br/>
<input type="submit" value="area"/>
</form><br>
结果
<body>
area : <s:property value="threeedge"/> <s:property value="threeedge.area"/> <br>
</body>
已解决。 展开
Action代码:
public class TriangleArea extends ActionSupport implements ModelDriven<ThreeEdge>{
ThreeEdge threeedge = new ThreeEdge();
public ThreeEdge getThr() {
return threeedge;
}
public void setThr(ThreeEdge thr) {
this.threeedge = thr;
}
public String execute(){
threeedge.setS((threeedge.getA()+threeedge.getB()+threeedge.getC())/2.0);
threeedge.setArea((threeedge.getS() - threeedge.getA()) * (threeedge.getS() - threeedge.getB())*(threeedge.getS() - threeedge.getC()) * threeedge.getS());
System.out.println(threeedge.getArea());
return "success";
}
public ThreeEdge getModel() {
return null;
}}
封装
public class ThreeEdge{
private double a,b,c,area,s;
省略 get/set方法
}
自定义转换类
public class TriangleToConvert extends StrutsTypeConverter {
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
System.out.println("正在转换:从字符串转换到复合类型...");
ThreeEdge thr = new ThreeEdge();
String param = arg1[0];
String[] paramValues = param.split(",");
thr.setA(Double.parseDouble(paramValues[0]));
thr.setB(Double.parseDouble(paramValues[1]));
thr.setC(Double.parseDouble(paramValues[2]));
System.out.println(thr.getA());
return thr;}
public String convertToString(Map arg0, Object arg1) {
ThreeEdge thr = (ThreeEdge)arg0;
String result= "边1:" + thr.getA() + "边2:" + thr.getB() + "边3:" + thr.getC();
return result;
}}
文件
TriangleArea-conversion.properties里
threeedge=cn.edu.util.TriangleToConvert
jsp页面
<form id="form1" action="TriangleArea.action" name="form1" method="post">
<label>
三边:<input type="text" name="threeedge"/>
</label><br/>
<input type="submit" value="area"/>
</form><br>
结果
<body>
area : <s:property value="threeedge"/> <s:property value="threeedge.area"/> <br>
</body>
已解决。 展开
展开全部
1.配置文件必须放置在Action的同目录下
2.配置文件的名称是有Action对象的名称+"-conversion.properties"
3.配置文件的编写采用key-value的形式
4.key则是:Action中对应接收数据的成员变量名称
5.value则是:自建转换器的限定名
有没有错误信息?
2.配置文件的名称是有Action对象的名称+"-conversion.properties"
3.配置文件的编写采用key-value的形式
4.key则是:Action中对应接收数据的成员变量名称
5.value则是:自建转换器的限定名
有没有错误信息?
追问
public String convertToString(Map arg0, Object arg1) 请问这个方法最后的result 是给返回JSP了还是?
追答
由struts来调用,具体细节上你可以看下StrutsTypeConvert的源码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询