在JSP开发中,如果有错误信息,需要输出具体的出错信息,这时候用的是Properties文件。问题是在Properties文件中只能使用固定的语句,我现在需要能够传入参数,比如下面的例子:在JSP文件中的代码如下:
<tr>
<td><bean:message key="addUser.prompt.ips" /></td>
<td><html:textarea property="ipArrayStr" value="<%= ipArrayStr %>" />
<html:errors property="ipArrayStr" />
</td>
</tr>在Properties文件中的errors代码如下:
addUser.errors.ipArrayStr.invalid=<<< IP is invalid !现在有个问题就是如果同时出现n个IP错误,此时就会出现n句“<<< IP is invalid !”。这不是想要的结果,我所希望的结果是能够出现:
<<< IP “123.251.561.1" is invalid !这样的信息,也就是说将错误的IP值“123.251.561.1"也一同在JSP页面中显示出来。谢谢啦!
<tr>
<td><bean:message key="addUser.prompt.ips" /></td>
<td><html:textarea property="ipArrayStr" value="<%= ipArrayStr %>" />
<html:errors property="ipArrayStr" />
</td>
</tr>在Properties文件中的errors代码如下:
addUser.errors.ipArrayStr.invalid=<<< IP is invalid !现在有个问题就是如果同时出现n个IP错误,此时就会出现n句“<<< IP is invalid !”。这不是想要的结果,我所希望的结果是能够出现:
<<< IP “123.251.561.1" is invalid !这样的信息,也就是说将错误的IP值“123.251.561.1"也一同在JSP页面中显示出来。谢谢啦!
addUser.errors.ipArrayStr.invalid=IP [{0}] is invalid!
然后程序中根据\{\}做正则来替换掉{0}.
在代码<html:errors property="ipArrayStr" />中无法插入替换语句啊。还是要在Action里面就完成?谢谢啦!
import java.util.Map;
import java.util.HashMap;public class Main {
public static void main(String[] args) {
Map<String, String> keyMap = new HashMap<String, String>(); keyMap.put("0", "用户名");
keyMap.put("1", "密码"); String f_message = "抱歉,您的{0}和{1}都不正确!"; for (int i = 0; i < keyMap.size(); i++) {
f_message = f_message.replace("{" + i + "}", keyMap.get(String.valueOf(i)));
} System.out.println(f_message);
}
}
addUser.errors.ipArrayStr.invalid= < < < IP <font color=red>{0}</font> is invalid !在Action中:
String IP="特定的错误IP";
ActionMessages errors=new ActionMessages();
ActionMessage error=new ActionMessage("addUser.errors.ipArrayStr.invalid",IP);
errors.add("invalidIP",error);
this.saveErrors(request, errors);在Jsp中:
<html:errors property="invalidIP"/>
if (this.ipArrayStr.length() != 0){
String[] ipStrArray = ipArrayStr.split("\r\n");
for (int i = 0; i < ipStrArray.length; i++) {
if (ipStrArray[i].length() != 0)
ipArrayStrOut = ipArrayStrOut + ipStrArray[i] + "\r\n";
StrutsFormValidationHelper.isIP(errors, this.formName, "ipArrayStr", ipStrArray[i]);
try {
gnu.regexp.RE regexpValid = new RE("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
if (!regexpValid.isMatch(ipStrArray[i])) {
wrongIpStr = wrongIpStr + ipStrArray[i] + "; ";
}
} catch (REException exception) {
exception.printStackTrace();
} }
}
return errors;
就好像他全部都在Form里面验证完成就直接输出错误信息,并且停留在当前页面。那他的error信息是传送到什么地方去了?谢谢!
if (this.ipArrayStr.length() != 0){
String[] ipStrArray = ipArrayStr.split("\r\n");
for (int i = 0; i < ipStrArray.length; i++) {
if (ipStrArray[i].length() != 0)
ipArrayStrOut = ipArrayStrOut + ipStrArray[i] + "\r\n";
StrutsFormValidationHelper.isIP(errors, this.formName, "ipArrayStr", ipStrArray[i]);
try {
gnu.regexp.RE regexpValid = new RE("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
if (!regexpValid.isMatch(ipStrArray[i])) {
wrongIpStr = wrongIpStr + ipStrArray[i] + "; ";
}
} catch (REException exception) {
exception.printStackTrace();
} }
}
return errors;
就好像他全部都在Form里面验证完成就直接输出错误信息,并且停留在当前页面。那他的error信息是传送到什么地方去了?谢谢!
struts好久不用,有些地方我也忘了,说错了不要见怪哦!这个地方不理解应该是struts框架的处理流程还不熟悉!一个Form提交的时候,先是到servletAction 然后被封装成FormBean,然后验证这个bean,或者通过validate(不知道是不是这么写的!),如果没有通过直接返回到原来的jsp页面,如果通过则将整个bean传递给action,在action处理完成后再返回给servletAction,由其判断跳转到哪里!
porperties里面的带参数,如"hello %S",然后用输入参数给printf。