看来我无法理解如何在 Jersey 中实现自定义 ViewProcessor。我有以下资源:
@Path("/events")
public class EventController extends AbstractController {
private static final Logger LOG = LoggerFactory.getLogger(EventController.class);
@Resource(name = "eventService")
private EventService eventService;
public void setEventService(EventService eventService) {
this.eventService = eventService;
}
@GET
@Produces(MediaType.TEXT_HTML)
public Viewable viewEventsAsHtml() {
Map<String, String> model = new HashMap<String, String>();
model.put("msg", "test");
return new Viewable("/events.jsp", model);
}
}
因为我想在“/WEB-INF/views/jsp”中组织我的jsp视图,所以我扩展了JSPTemplateProcessor:
public class CJJspTemplateProcessor extends JSPTemplateProcessor {
private static final Logger LOG = LoggerFactory.getLogger(CJJspTemplateProcessor.class);
private final String basePath = "/WEB-INF/views/jsp";
public CJJspTemplateProcessor(@Context ResourceConfig resourceConfig) {
super(resourceConfig);
}
@Override
public String resolve(String name) {
String path;
if (!name.endsWith(".jsp")) {
name += ".jsp";
}
path = basePath + name;
LOG.debug("resolving " + name + " to " + path);
return path;
}
}
但是当我导航到事件资源时,我得到一个 404,虽然我可以看到 TemplateProcessor 为日志文件中的 jsp 解析了正确的路径。
请帮助:)