我试图将一个值ferm jsp传递回bean。我创建了一个包含多个参数的动作网址,它们都设置正确,但公司名称设置为" companyName,companyName"而不只是" companyName"
这是我的网址
var url="<liferay-portlet:actionURL><liferay-portlet:param name='struts.portlet.action' value='/reg/submitStep1'/><liferay-portlet:param name='isSimplified' value='true'/></liferay-portlet:actionURL>&customerNo=" + custNo + "&companyName=" + companyName;
我使用jquery从文本字段中获取公司名称变量。文本输入的id是companyName
var companyName = $('#companyName').val();
当我打印出using console.log(companyName);
时,该值很好,没有连接两次。
但是,在setter中,String的值设置为&#34; companyName,companyName&#34;。所有其他参数(isSimplified
,customerNo
)都可以。知道为什么会这样吗?
答案 0 :(得分:0)
您正在将companyName作为url参数和表单参数传递,因此您将在服务器端获取这两个参数。
只保留一个参数作为url参数或表单参数。
答案 1 :(得分:0)
我正在将我的健康领域应用程序从Struts 1.x迁移到Struts 2,这是一个非常庞大且成熟的应用程序。 所以最后我创建了下面的工作代码来克服连接值两次被逗号隔开的情况,如果传递重复的参数的话。 我在Util类中创建了以下通用方法来克服 在我整个应用程序中,所有模型都存在这个问题, 从您的Action类validate方法调用此方法 注意:它将优先于url参数而不是form参数,这是我的要求。 这是我自定义的方法:
public static void refineModel(HttpServletRequest request , Object modelObject) {
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) {
String parameterName = (String) enumeration.nextElement();
String parameterValue = request.getParameter(parameterName);
try {
if (modelObject.getClass().getDeclaredField(parameterName) != null &&
String.class.isAssignableFrom(modelObject.getClass().getDeclaredField(parameterName).getType())) {
Field field = modelObject.getClass().getDeclaredField(parameterName);
field.setAccessible(true); // to access private variables also
field.set(modelObject, parameterValue );
}
} catch (Exception e) {
//not required
}
}
}
答案 2 :(得分:0)
为了克服这个问题,最终我创建了拦截器并在堆栈中的模型拦截器之后调用它
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
ValueStack stack = invocation.getStack();
StringBuffer sb = new StringBuffer();
Iterator<Object> iterator = stack.getRoot().iterator();
try {
while (iterator.hasNext()) {
Object actionObject = iterator.next();
try {
if (ActionSupport.class.isAssignableFrom(actionObject.getClass())) {
if (invocation.getAction().getClass().getMethod("getModel", null) != null) {
Method modelMethod = invocation.getAction().getClass().getMethod("getModel", null);
Object modelObject = modelMethod.invoke(actionObject, null);
sb.append("\n*********RefineModelInterceptor**************");
sb.append("\n********** " + modelObject.getClass().getName());
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) {
String parameterName = (String) enumeration.nextElement();
String parameterValue = request.getParameter(parameterName);
try {
if (parameterName.matches("^(.*)\\[\\'(.*)\\'\\]$")) {
String mapName = parameterName.substring(0, parameterName.indexOf("["));
String mapIndex = parameterName.substring((parameterName.indexOf("'") + 1), parameterName.lastIndexOf("'"));
if (modelObject.getClass().getDeclaredField(mapName) != null) {
Field field = modelObject.getClass().getDeclaredField(mapName);
field.setAccessible(true); // to access private variables also
if (HashMap.class.isAssignableFrom(modelObject.getClass().getDeclaredField(mapName).getType())) {
HashMap<String, String> tempHashMap = (HashMap) field.get(modelObject);
String hashMapValue = tempHashMap.get(mapIndex);
sb.append("\n"+parameterName+" **earlier** " + hashMapValue);
sb.append("\n"+parameterName+" ****now**** " + parameterValue);
tempHashMap.put(mapIndex, parameterValue);
field.set(modelObject, tempHashMap);
} else if (TreeMap.class.isAssignableFrom(modelObject.getClass().getDeclaredField(mapName).getType())) {
TreeMap<String, String> tempHashMap = (TreeMap) field.get(modelObject);
String treeMapValue = tempHashMap.get(mapIndex);
sb.append("\n"+parameterName+" **earlier** " + treeMapValue);
sb.append("\n"+parameterName+" ****now**** " + parameterValue);
tempHashMap.put(mapIndex, parameterValue);
field.set(modelObject, tempHashMap);
}
}
} else if (modelObject.getClass().getDeclaredField(parameterName) != null && String.class.isAssignableFrom(modelObject.getClass().getDeclaredField(parameterName).getType())) {
Field field = modelObject.getClass().getDeclaredField(parameterName);
field.setAccessible(true); // to access private variables also
sb.append("\n"+parameterName+" **earlier** " + field.get(modelObject));
boolean notFoundSetterMethod=true;
/* below code is to invoke the setter method so that if any functionality return in setter method could be sustained
if failed in finding setter method than directly set value on the field
*/
try {
Method method = modelObject.getClass().getMethod("set" + org.apache.commons.lang3.StringUtils.capitalize(field.getName()), String.class);
notFoundSetterMethod=false;
method.invoke(modelObject, parameterValue);
sb.append("\n"+parameterName+" ****now**** " + field.get(modelObject));
} catch (Exception ex) {
notFoundSetterMethod=true;
//not required
}
if(notFoundSetterMethod){
field.set(modelObject, parameterValue);
sb.append("\n"+parameterName+" ****now**** " + field.get(modelObject));
}
}
} catch (Exception e) {
//not required
}
}
}
//break; .. should we really break since got the action object ?
}
} catch (Exception e) {
//not required
}
}
} catch (Exception e) {
//not required
}
sb.append("\n**************************************\n");
logger.debug(sb.toString());
return invocation.invoke();
}
下面是我的堆栈:
<interceptor-stack name="myCustomizedDefaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="datetime"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="refineModelInterceptor"/> <!-- custom interceptor for removing duplicate parameter issue-->
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
<interceptor-ref name="debugging"/>
</interceptor-stack>