2013-05-28 69 views
0

我想使用Web表单来提交一些信息,然后JSP页面将显示输入的表单信息。但是,当我单击表单中的提交按钮时,它将转到正确的JSP文件,但所有表单值都显示为“null”。我使用Jersey来执行POST请求。为什么我会在JSP中获得空值?

形式为:

<form action="/MyRestWS/rest/customer/created" method="POST"> 
    <table border="1"> 
     <tr> 
      <td>Customer name:</td> 
      <td><input type="text" name="name"></td> 
     </tr> 
     <tr> 
      <td>Customer ID:</td> 
      <td><input type="text" name="id"></td> 
     </tr> 
     <tr> 
      <td>Customer DOB:</td> 
      <td><input type="text" name="dob"></td> 
     </tr> 
    </table> 
    <br/> 
    <input type="submit" value="Submit"> 
</form> 

做请求的代码是:

@Path("/customer") 
public class CustomerService { 

    @POST 
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    @Path("created") 
    public Response createCustomer(@FormParam("id") int id, @FormParam("name") String name, @FormParam("dob") Date dob) { 
     Response r; 

     r = Response.ok().entity(new Viewable("/confirm.jsp")).build(); 
     return r; 
    } 

    @GET 
    @Produces(MediaType.TEXT_HTML) 
    public Viewable displayForm() { 
     return new Viewable("/form.html"); 
    } 
} 

显示的JSP文件是confirm.jsp,其内容为:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Your entered information</title> 
</head> 
<body> 
    <h2> 
     <% 
      out.println("You've entered the following information:"); 
     %> 
    </h2> 

    <p> 
     Customer Name: 
     <%=request.getParameter("name")%></p> 
    <p> 
     Customer ID: 
     <%=request.getParameter("id")%></p> 
    <p> 
     Customer DOB: 
     <%=request.getParameter("dob")%></p> 
</body> 
</html> 

如果我在浏览器中键入以下地址:

http://localhost:8080/MyRestWS/rest/customer 

它会告诉我form.html的形式。我填写信息,然后单击后“提交”,它会去以下地址,并通过指定的路径显示JSP文件:

http://localhost:8080/MyRestWS/rest/customer/created 

的JSP文件显示正确,但所有的客户信息字段显示为“null”如下:

You've entered the following information: 

Customer Name: null 

Customer ID: null 

Customer DOB: null 

那么为什么我在提交表单后在JSP中得到空值?我的代码有什么问题?

+1

**无关:** [在JSP中使用scriptlets非常令人沮丧](http://stackoverflow.com/a/3180202/814702) – informatik01

+1

@ informatik01:感谢您的建议。我只是使用JSP来获得快速原型。 – tonga

+0

我明白了。我写了它以防万一)) – informatik01

回答

3

您试图显示不再存在的请求参数;它们仅存在于第一个请求的持续时间内,表单提交。

如果要再次显示它们您需要将它们暴露给视图层,请求属性

(这就是说,我会更开心,如果他们被暴露作为构造类的一部分。)

+0

谢谢戴夫。你能给我一个例子公开表单参数作为请求属性吗? – tonga

0

继戴维的建议,我已经添加了下面的请求属性在createCustomer()方法和所用request.getAttribute()confirm.jsp来检索输入的表单数据。它终于有效。提交表单后,confirm.jsp文件可以正确显示所有信息。

request.setAttribute("name", name); 
    request.setAttribute("dob", dob); 
    request.setAttribute("id", Integer.valueOf(id)); 
    RequestDispatcher dispatcher = request.getRequestDispatcher("/confirm.jsp"); 
    dispatcher.forward(request, response); 

然而,一边的问题是:我不得不使用下面的注射类字段:

@Context HttpServletRequest request; 
@Context HttpServletResponse response; 

如果我用,而不是下面,我将获得异常错误:

@Context ServletRequest request; 
@Context ServletResponse response; 

这是为什么?

相关问题