📄 feedformcontroller.java
字号:
package org.openfans.web;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.PropertyUtils;
import org.openfans.domain.Facade;
import org.openfans.domain.Fans;
import org.openfans.domain.FansFeed;
import org.openfans.domain.Feed;
import org.openfans.domain.OthersFeed;
import org.openfans.domain.TagFeed;
import org.openfans.util.ThreadFans;
import org.openfans.web.form.FeedForm;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class FeedFormController extends SimpleFormController {
private Facade facade = null;
public void setFacade(Facade facade) {
this.facade = facade;
}
public FeedFormController() {
setCommandName("feedForm");
setCommandClass(FeedForm.class);
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
FeedForm feedForm = (FeedForm) command;
String tag = feedForm.getTagStr();
String type = feedForm.getType();
if (feedForm.getFeedId() != 0) {// 已经存在
Feed f=facade.getFeed(feedForm.getFeedId());
PropertyUtils.copyProperties(f, command);
facade.save(f);
} else {
Feed feed = null;
if (type != null && !"".equals(type)) {
if ("1".equals(type)) {
feed = new FansFeed();
PropertyUtils.copyProperties(feed, command);
} else if ("2".equals(type)) {
feed = new TagFeed();
PropertyUtils.copyProperties(feed, command);
((TagFeed) feed).setTag(facade.getTagByTitle(tag));
} else if ("3".equals(type)) {
feed = new OthersFeed();
PropertyUtils.copyProperties(feed, command);
}
} else {
feed = new Feed();
PropertyUtils.copyProperties(feed, command);
}
Fans fans = facade.getFans(ThreadFans.get().getFansId());
feed.setLastDate(new Date(100, 1, 1));
if (feed instanceof TagFeed) {
facade.saveFeed(fans, feed);
// 需将feed存入数据库,再将其加入tag的feed列表中
((TagFeed) feed).getTag().addFeed((TagFeed) feed);
} else {
facade.saveFeed(fans, feed);
}
}
return new ModelAndView("redirect:editFans.html");
}
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
ThreadFans.authorize();
String feedId = request.getParameter("id");
if ((feedId != null) && !feedId.equals("0")) {
Feed feed = facade.getFeed(Integer.parseInt(feedId));
FeedForm feedForm = new FeedForm();
PropertyUtils.copyProperties(feedForm, feed);
if (feed instanceof FansFeed) {
feedForm.setType("1");
} else if (feed instanceof TagFeed) {
feedForm.setType("2");
feedForm.setTagStr(((TagFeed) feed).getTag().getTitle());
} else if (feed instanceof OthersFeed) {
feedForm.setType("3");
}
return feedForm;
}
return new FeedForm();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -