Struts 2和业务对象 [英] Struts 2 and business objects
问题描述
在Struts 2类中, http get params由字段变量自动提取。虽然在许多类中重复了类似 userId,groupId,
等类字段,但我决定制作一个业务对象类 RequestParams 每个班级都有code>
,并将所有字段放在那里。
In a Struts 2 class where http get params are auto fetched by field variables. While there were repeated such class fields like userId,groupId,
etc in many classes, I decided to make one business object class RequestParams
in each class and put all the field there.
然后我的所有课程都只有 RequestParams rp;
, getRp();
和 setRp();
rp类将使用getter / setter和 userId
所有其他领域。
Then all my class will have just the RequestParams rp;
with getRp();
and setRp();
the rp class will have the userId
with getters / setters and all other fields.
现在我看到我必须更换。例如 userId
, getRp()
。 getUserId();
第34行现在代码看起来很难看。
Now I see I have to replace. e.g userId
with getRp()
.getUserId();
at line 34 Now the code is looking ugly.
使用: messageId = ChatDao.saveMessage(userId,groupId,message);
看起来像
rp.setMessageId( ChatDao.saveMessage(rp.getUserId(), rp.getGroupId(), rp.getMessag() ) );
这样做的更好方法是什么?
what is a better way of doing such things?
public class SendMessageOrStartChatAction extends BaseActoinSupport{
private static final long serialVersionUID = 1L;
private int userId;
private int groupType;
private int groupId;
private String groupTitle;
private String groupMemberIds;
private int randomCode;
private String message;
private int messageId; //internal class ues
@Override
/** boo */
protected void doExecute() throws IOException {
//check if it had random code in db, (msg already saved in db)
if(ChatDao.randomCodeExists(randomCode)){
messageId = ChatDao.getMessageIdThatMatchesRandomCode(randomCode);
write(messageId);
}else{
if(groupId <= 0){
//create group
groupId = ChatDao.createChatGroup(userId, groupTitle, groupType);
String[] memberIdsArray = groupMemberIds.split("==");
ChatDao.addUsersToGroup(groupId, memberIdsArray);
}
//save message
messageId = ChatDao.saveMessage(userId,groupId , message);
// queued: may be put this in last viewed messages here. may be.
write(messageId);
}
}
}
推荐答案
这种方法没有错,如果你聚合一个类并想要访问它的属性,那么公共访问器适合你,你也可以通过OGNL访问它们。该操作位于 valueStack
之上,因此表达式看起来会更简单rp.userId
。无论如何,没有必要将所有参数传递给方法,您可以使用简化的方法签名
Nothing wrong with this approach, if you aggregate a class and want to access its properties then public accessors are appropriate to you and you could also access them via OGNL. The action is on top of the valueStack
, so expression will look much simpler "rp.userId"
for example. Anyway there's no need to pass all params to the method, you can use a simplified method signature
ChatDao.saveMessage(rp);
并在方法内部访问这些参数。
and inside the method access those parameters.
这篇关于Struts 2和业务对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!