如何在css中获取到Action中的值
今天做了一个简单的实验,用户在输入框中输入三个0~255之间的正整数,作为RGB的值,传到Action是一个字符串。
然后用逗号将其分开,分别赋值到三个int类型的变量:R,G,B,在传到另外一个jsp页面,动态改变字体颜色。
这个是输入RGB值得jsp页面
<form action="ColorChangeAction" align="center" name="colorchange" method="post">
<input type="text" name="color" placeholder="请输入三个0~255之间的正整数,用逗号分隔"/><br><br>
<input type="submit" value="提交">
</form>
<s:fielderror fieldName="numbererror"></s:fielderror>
这是用来业务处理的Action类的execute()方法,我们将R,G,B的值放到了request中
@Override
public String execute() throws Exception {
String[] colors = color.split(",");
try{
if(colors.length == 3){
R = Integer.parseInt(colors[0]);
G = Integer.parseInt(colors[1]);
B = Integer.parseInt(colors[2]);
Map request = (Map) ActionContext.getContext().get("request");
request.put("R", R);
request.put("G", G);
request.put("B", G);
return "success";
}else{
this.addFieldError("numbererror", "请输入三个0~255之间的正整数,用逗号分隔");
return "input";
}
}catch(NumberFormatException e){
this.addFieldError("numbererror", "请勿输入非法字符,请输入三个0~255之间的正整数,用逗号分隔");
return "input";
}
}
struts.xml
<action name="ColorChangeAction" class="com.lanling.action.ColorChangeAction">
<result name="success" >/success.jsp</result>
<result name="input">/inputRGB.jsp</result>
</action>
success.jsp
可见我们在css中直接使用EL表达式拿到了存放在request中的R,G,B。