3

看来我无法理解如何在 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 解析了正确的路径。

请帮助:)

4

1 回答 1

2

您不需要自定义 TemplateProcessor 来执行此操作,只需将以下内容添加到 web.xml 中的过滤器元素。

<filter>
    <init-param>
        <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
        <param-value>/WEB-INF/views/jsp</param-value>
    </init-param>
</filter>
于 2010-05-20T20:42:51.773 回答