问题描述
我在Action类中使用了ModelDriven
,以前它可以正常工作,现在它已经停止工作
I have used ModelDriven
in my Action class, previously it was working fine, Now it has stopped working
当我在jsp中使用Struts的<s:debug>
标记时,发现以下结果...
when I used <s:debug>
tag of Struts in my jsp I found following result...
---------更新的图像-----------
我在jsp页面中访问的值是:
I'm accessing values in my jsp page as :
<s:property value="categoryName"/>
<s:property value="categoryId"/> // I typed here
工作正常,如果我以以下方式访问它们:
working fine, If I accessed them as :
<s:property value="category.categoryName"/>
<s:property value="category.categoryId"/>
我的问题是:
- 为什么我要获得2次房产?
- 如何避免它们出现多次?
- 我看到很多人建议避免使用 ModelDriven ,为什么?
- Why I'm getting properties 2 times ?
- How to avoid them getting multiple times ?
- I have saw many people suggesting to avoid ModelDriven, why ?
-------更新问题-------
-
实际上我获得了3次属性,但我忘记突出显示第3个属性,请Plz看看我的 Action类
我尚未定义Model class
的单独属性,在我的操作中,突出显示为1 ,
如何设置这些属性?
I have not defined separate properties of Model class
In my action, highlighted by 1,
how to set these properties ?
是什么,实现ModelDriven
时我做错了吗?
Is anything, I'm doing wrong when implementing ModelDriven
?
这是我的模型 CategoryAction :
public class CategoryAction extends ActionSupport implements ModelDriven<Category>,
Preparable, SessionAware, ServletRequestAware, ServletContextAware {
private static final long serialVersionUID = 1L;
private Category category;
private Category [] categories;
private ServletContext servletContext;
private Map<String, Object> session;
private ServletRequest request;
@Override
public Category getModel() {
return this.category;
}
@Override
public void prepare() throws Exception {
this.category = new Category();
if( this.jmain == null )
this.jmain = new Jmain();
}
public void prepareCrudCategory() throws Exception {
this.categoryService = new CategoryService();
}
@Override
public String execute() throws Exception {
System.out.println("----------CategoryAction#execute()-----------");
if( this.category.getCategoryId() == 0)
this.category = this.jmain.getCategory( 1 ); //Get Main Category
else
this.category = this.jmain.getCategory( this.category.getCategoryId() );
System.out.println(this.category.toString());
return super.execute();
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Category[] getCategories() {
return categories;
}
public void setCategories(Category[] categories) {
this.categories = categories;
}
}
-------更新2 -------
这是我的 JSP 代码段
-------Update 2-------
this is my JSP code snippet
<form action="add-category" id="id-add-id-entry-form" name="addIdEntry" method="post">
<input type="hidden" id="opType" name="opType" value='<s:property value="opType"/>'>
<br /> <br />
<center>
<span id="id-message-span">
<s:if test="hasActionMessages()">
<h3 style="font-size: 22px; color: #FD0006; text-shadow: 2px 2px 3px rgba(255, 255, 255, 0.1); margin-left: 330px;">
<s:actionmessage />
</h3>
</s:if>
<s:if test="hasActionErrors()">
<h3 style="font-size: 16px; color: #FD0006; text-shadow: 2px 2px 3px rgba(255, 255, 255, 0.1); margin-left: 330px;">
<s:actionerror />
</h3>
</s:if>
</span>
<div id="id-id-entry-div" class="class-id-entry-div class-center">
<fieldset style="height: 100%; border-style: solid; border-width: 2px; border-color: #FEC458;"
>
<legend class="PageTitleUpperCase"> Add Category </legend>
<table cellspacing="5" cellpadding="2" border="0" width="65%" class="class-center">
<tr>
<td>
<span style="color: red">*</span>
<label class="Label_Green"> Patent Category Name :</label>
<s:if test=""></s:if>
</td>
<td>
<s:property value="opType"/><br>
<s:property value="categoryId"/>
<s:property value="categoryName"/>
<s:if test="%{opType == 0}">
<%-- <s:property value="categoryName"/> --%>
<input type="hidden" name="parentCategoryId" value='<s:property value="categoryId"/>' >
<label class="Label_Green">
<s:property value="categoryName"/>
</label>
</s:if>
<s:else>
<%-- <s:property value="parentCategoryName"/> --%>
<input type="hidden" name="parentCategoryId" value='<s:property value="parentCategoryId"/>' >
<label class="Label_Green">
<s:property value="parentCategoryName"/>
</label>
</s:else>
</td>
</tr>
<tr>
<td>
<span style="color: red">*</span>
<label class="Label_Green"> Category Name :</label>
<s:if test=""></s:if>
</td>
<td >
<input type="hidden" name="categoryId"
<s:if test="%{opType == 0}">value='0'</s:if>
<s:else>value='<s:property value="categoryId"/>'</s:else>
>
<input id="id-category-name-text" type="text" name="categoryName"
required="required" size="40" placeholder="Enter Category Name Here..."
<s:if test="%{opType == 1}">value='<s:property value="categoryName"/>'</s:if> >
</td>
</tr>
<tr>
<td></td>
<td align="left"><input type="submit" id="submit_img"
<s:if test="opType != 0"> value='Update'</s:if>
<s:else> value='Submit' </s:else> >
</td>
</tr>
</table>
</fieldset>
</div>
<s:debug/>
</center>
</form>
____更新有关如何使用调试标记获取价值栈内容的图像_____
使用<s:debug/>
后,单击它获得了[debug]
链接,显示了以下值:
After using <s:debug/>
, I got [debug]
link on clicking it , following values are Shown :
<s:debug/>
表示列表类别页面,点击后点击[debug]
链接,显示以下值:
<s:debug/>
for List Category Page, [debug]
link on clicking it , following values are Shown :
推荐答案
ModelDriven
将模型推入值堆栈的顶部,因此堆栈中同时包含模型和动作. ValueStack
是由Struts框架实现的接口,该接口允许在处理请求时处理数据.您可以从此处中阅读.
ModelDriven
pushes the model onto the top of the value stack, so you have both a model and action in the stack. ValueStack
is an interface implemented by Struts framework that allows to manipulate data while the request is processed. How it works you can read from here.
您可以在valueStack的不同级别上找到具有相同名称/键的属性,从上到下的搜索顺序一直到找到该值为止.
You can find properties with the same name/key on the different levels of the valueStack the order of searching from top to down the stack until the value is found.
ModelDriven
操作简化了到表单对象到模型对象的映射,但是如果您需要对同一操作类进行多个模型操作,则可能会遇到困难.您可能在验证,类型转换以及Struts框架的其他与拦截器一起使用的功能方面遇到困难.而且,如果不能解决问题,而不是从已实现的接口中删除ModelDriven
或重新配置拦截器堆栈以禁用modelDriven
拦截器,那么就可以不用ModelDriven
.
ModelDriven
action simplifies mapping form fields to the model object, but you might have difficulties if you need to do multiple models to the same action class. You might have difficulties with validation, type conversion, other features of the Struts framework that work with interceptors. And if can't resolve your problems rather than removing ModelDriven
from the implemented interfaces or reconfiguring the interceptor stack to disable modelDriven
interceptor, then you can live without ModelDriven
.
这篇关于ModelDriven停止在Struts2中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!