问题

在之前的学习中,表单中的属性只要和后台的Action类中的属性的名称一致的话,就可以通过值栈传值过去,但是有一个问题,前台的属性的类型都是String类型的,而后台的有String类型的,有int类型的,但是都可以,这个是什么在运作呢?把字符串类型和基本数据类型进行了转化呢?

答案

就是我们今天要学习的内容。


有一个疑问?

为什么表单设置的属性名称和Action类的属性名称一致,就可以传值过去,怎么传值的?

解答

在提交表单的时候根据action=\"地址\",找到struts. 配置文件,根据里面的配置找到符合条件的地址的Action类,

在这个Action类里面,主要是拦截器在工作,paramsPrepareParamsStack拦截器首先给对应的属性赋值,然后调用Modeldriven,

如果没有继承ModelDriven<Employ>接口重写getModel()方法的话,就需要手动的将封装这些属性的类的对象置于值栈栈顶,方便另一个页面调用。


今日的学习内容

Struts2类型转化错误的显示和定制

demo说明:

1、表单提交一个年龄值,在后台打印出来

2、提交12,正常打印;提交aa,打印0;(明明出错了,但是运行机制跟什么也没有发生一样,这个不是我们想要的,我们想要发生什么,我们都可以知道)

3、继承InvalidationAware接口的话,就会显示错误信息,但是前提是有<result name=\"input\"></result>有name=\"input\"的reslut

这个例子的代码与结果截图如下

Convert.jsp

<body>
   <s:form action=\"convert\">
   <s:textfield name=\"age\" label=\"年龄\"></s:textfield>
   <s:submit></s:submit>
   </s:form>
  </body>

Struts.

<action name=\"convert\" class=\"cn.com.action.ConvertTest\">
<result>/success.jsp</result>
<result name=\"input\">/Convert.jsp</result>
</action>

ConvertTest类

package cn.com.action;

import com.opensymphony.xwork2.ActionSupport;

public class ConvertTest extends ActionSupport{
private Integer age;
public void setAge(Integer age) {
	this.age = age;
}
public Integer getAge() {
	return age;
}
public String execute(){
	System.out.println(\"age:\"+age);
	return \"success\";
}
}

 

在jsp页面输入12,后台正常打印

\"\"

在jsp页面输入aa

\"\"


 通过以上的例子,我们有了如下的疑问

1、怎么覆盖错误信息?

1)在对应的Action类里面新建一个Action[classname].properties文件,

2)在属性文件里面添加这样的一个键值对:

invalid.filedvalue.fileName=xxx;

\"\"

\"\" 

2、如果表单的主题是simple,还会打印错误信息么?如果不能打印错误信息,该怎么办?

  <body>
   <s:form action=\"convert\" theme=\"simple\">
   age:<s:textfield name=\"age\" label=\"年龄\"></s:textfield>
   <br><br>
   <s:submit></s:submit>
   </s:form>
  </body>

不显示错误信息

于是我们用debug查看一下·

\"\"

 

 在页面上加上 ${fieldErrors.age[0]}就可以显示错误信息了;

\"\"

但是问题来了,在simple主题里面,显示错误信息会有多余的li,ul格式。

解决方案:

在src下面新建一个包,包名template.simple,在这个包下面新建一个fielderror.ftl

把struts2-core-2.3.36.jar下面的template.simple下面的fielderror.ftl下面的原生代码拷贝过来,放在src下面的fielderror.ftl里面;

然后把里面的ul,li都去除掉就可以了

我剔除ul,li后的代码如下

<#--
/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * \"License\"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<#if fieldErrors??><#t/>
    <#assign eKeys = fieldErrors.keySet()><#t/>
    <#assign eKeysSize = eKeys.size()><#t/>
    <#assign doneStartUlTag=false><#t/>
    <#assign doneEndUlTag=false><#t/>
    <#assign haveMatchedErrorField=false><#t/>
    <#if (fieldErrorFieldNames?size > 0) ><#t/>
        <#list fieldErrorFieldNames as fieldErrorFieldName><#t/>
            <#list eKeys as eKey><#t/>
                <#if (eKey = fieldErrorFieldName)><#t/>
                    <#assign haveMatchedErrorField=true><#t/>
                    <#assign eValue = fieldErrors[fieldErrorFieldName]><#t/>
                    <#if (haveMatchedErrorField && (!doneStartUlTag))><#t/>
                        <#assign doneStartUlTag=true><#t/>
                    </#if><#t/>
                    <#list eValue as eEachValue><#t/>
                       <#if parameters.escape>${eEachValue!?html}<#else>${eEachValue!}</#if>
                    </#list><#t/>
                </#if><#t/>
            </#list><#t/>
        </#list><#t/>
        <#if (haveMatchedErrorField && (!doneEndUlTag))><#t/>
            <#assign doneEndUlTag=true><#t/>
        </#if><#t/>
        <#else><#t/>
        <#if (eKeysSize > 0)><#t/>
      
            <#list eKeys as eKey><#t/>
                <#assign eValue = fieldErrors[eKey]><#t/>
                <#list eValue as eEachValue><#t/>
                   <#if parameters.escape>${eEachValue!?html}<#else>${eEachValue!}</#if>
                </#list><#t/>
            </#list><#t/>
        </#if><#t/>
    </#if><#t/>
</#if><#t/>

现在页面的布局没有那么难看了

\"\"

 

收藏 打印