泽西(jsr 311)&自定义ViewProcessor

时间:2010-05-20 13:00:28

标签: java rest jersey

似乎我无法解决如何在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的正确路径。

plx帮助:)

1 个答案:

答案 0 :(得分:2)

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

<filter>
    <init-param>
        <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
        <param-value>/WEB-INF/views/jsp</param-value>
    </init-param>
</filter>