如何将表单的参数发送到休息服务

时间:2015-12-06 11:41:08

标签: java html5 rest jax-rs

我正在尝试将表单的参数发送到休息服务。第一个用户使用方法:

请求具有表单元素的页面
@GET
    @Produces("text/html")
    @Path("/create")
    public Response create() {
        final StringBuilder sb = new StringBuilder();

        sb.append("<h1>Create a new Event</h1>");
        sb.append("<ul>");
        sb.append("<form method=\"POST\" action=\"http://localhost:9000/events/created" +"?_method=PUT\">");
        sb.append("<br>title</br><input type=\"text\" required name=\"eventtitle\"/>");
        sb.append("<br>type</br><input type=\"text\" required name=\"eventtype\"/>");
        sb.append("<br>description</br><input type=\"text\"required name=\"eventdescription\"/>");
        sb.append("<br>location</br><input type=\"text\" required name=\"eventlocation\"/>");
        sb.append("<br>event date</br><input type=\"text\" required name=\"eventdate\"/>");
        sb.append("<br>creation date</br><input type=\"text\" required name=\"eventcreationdate\"/>");
        sb.append("<br>modification date</br><input type=\"text\"required name=\"eventmodificationdate\"/>");
        sb.append("<br></br><input type=\"submit\" value=\"create event\"/>");
        sb.append("</form>");
        sb.append("<li><p><a href=\"/events\">go back</a></p></li>");
        sb.append("</ul>");
        return Response.status(200).entity(sb.toString()).build();

之后,我需要获取输入值并发送到方法:

@POST
    @Path("/created")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void createEvent( @FormParam("eventtitle") String title,
            @FormParam("eventtype") String type,
            @FormParam("eventdescription") String description,
            @FormParam("eventlocation") String location,
            @FormParam("eventdate") String date,
            @FormParam("eventcreationdate") String creationdate,
            @FormParam("eventmodificationdate") String modificationdate,
            @Context HttpServletResponse servletResponse) throws IOException {
        final StringBuilder sb = new StringBuilder();
        sb.append( "<h6>Event sucessfully created </h6>");
        sb.append("<br></br><li><p><a href=\"/events\">go back</a></p></li>");
        Response.status(200).entity(sb.toString()).build();
        eventWorker.createEvent(title,type,description,location,date,
                creationdate,modificationdate);

    }

但它根本不起作用。我有什么想法,我做错了什么?

提交后,网址为:http://localhost:9000/events/created?_method=PUT

0 个答案:

没有答案