我正在重构传统应用程序以使用Spring MVC。我的所有控制器(遗留)返回一个Model类型的对象,我的遗留调度程序编写model.getContent()的输出,方法getContent执行内部处理并返回一个json字符串。我有数百个控制器,不想重写它们。是否有可能编写自定义视图处理程序并将其包含在spring servlet配置中?Spring MVC自定义视图
样品控制器:
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(legacy mvc)。我最近在这个项目中引入了spring,并重构了使用依赖注入和请求作用域命令对象(请求包装)
非常感谢!非常简单的解决方案和作品 – user979051