【发布时间】:2013-06-03 19:18:57
【问题描述】:
我正在重构一个遗留应用程序以使用 Spring MVC。我所有的控制器(旧版)都返回一个 Model 类型的对象,而我的旧版调度程序编写 model.getContent() 的输出,getContent 方法进行内部处理并返回一个 json 字符串。我有数百个控制器,不想重写它们。是否可以编写自定义视图处理程序并将其包含在 spring servlet 配置中?
样品控制器:
public UserList extends BasicAction {
@Autowired
UserService userService;
@Autowired
UserCommand userCommand;
@Override
public Model getModel(Request req, Response resp)
throws ServletException, IOException {
Model model = new Model();
List<User> users;
try {
users = userService.getUsers((UserCriteria)userCommand.getResult());
model.addCollection(users);
model.setWrapper(new UserWrapper());
} catch (ValidationException e) {
e.printStackTrace();
} catch (WebCommandException e) {
e.printStackTrace();
}
return model;
}
}
我打算注释为@Controller。指定 @RequestMapping 或在 xml 配置中,删除基类 BasicAction(旧版 mvc)。我最近在这个项目中引入了 spring 并重构为使用依赖注入和请求作用域命令对象(请求包装器)
【问题讨论】:
标签: java spring model-view-controller