Passing parameters to action through ModelDriven in Struts 2

醉酒当歌 提交于 2019-11-26 23:09:11

The name is the property of the model and also the property of the action class. The modelDriven interceptor pushes the model on top of the value stack, so it is easy to use it in JSP. The action object is below the model. So, it could be referenced directly using [1] prefix. See OGNL basics.

But it's not necessary if there's no duplicate property names in the model and action object. When the name such as name is evaluated by OGNL it searches from the top of the value stack to down the stack for the property accessor. The first found accessor will be executed. So, the model property has a priority because the model is on top of the value stack.

If the property with the name name should be set on the action then you could directly name that property as [1].name. But, such parameter name is not accepted by default pattern of params interceptor. However, it is a valid OGNL expression. So, to get it pass through the interceptor you need to add it to a pattern of accepted parameter names. Like that

@Action(value="modelDrivenResult", results=@Result(location = "/modelDriven/modelDrivenResult.jsp"),
  interceptorRefs = @InterceptorRef(value="defaultStack", params={
    "params.acceptParamNames", "(\\[\\d+\\]\\.)*\\w+((\\.\\w+)|(\\[\\d+\\])|(\\(\\d+\\))|(\\['\\w+'\\])|(\\('\\w+'\\)))*"
  })
)

This is because OGNL also checks the pattern of accepted parameters and this regex pattern allows to match both params and OGNL matchers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!