在JSP我如何从URL获取参数?
例如,我有一个URL www.somesite.com/Transaction_List.jsp?accountID=5 我要得到5。 有什么要求吗?getAttribute("accountID")就像有会话或类似的东西吗?
在JSP我如何从URL获取参数?
例如,我有一个URL www.somesite.com/Transaction_List.jsp?accountID=5 我要得到5。 有什么要求吗?getAttribute("accountID")就像有会话或类似的东西吗?
在GET请求中,请求参数取自查询字符串(URL上问号后面的数据)。例如,URL http://hostname.com?p1=v1&p2=v2包含两个请求参数- - p1和p2。在POST请求中,请求参数来自查询字符串和在请求体中编码的已发布数据。
下面的例子演示了如何在生成的输出中包含一个请求参数的值:
Hello <b><%= request.getParameter("name") %></b>!
如果使用URL访问页面:
http://hostname.com/mywebapp/mypage.jsp?name=John+Smith
结果输出将是:
Hello <b>John Smith</b>!
如果查询字符串上没有指定name,输出将是:
Hello <b>null</b>!
下面的例子使用了scriptlet中查询参数的值:
<%
if (request.getParameter("name") == null) {
out.println("Please enter your name.");
} else {
out.println("Hello <b>"+request. getParameter("name")+"</b>!");
}
%>
request.getParameter("accountID")就是你要找的。这是Java Servlet API的一部分。更多信息请参见http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html。
关于统一表达式语言的隐式对象,Java EE 5教程写道:
Implicit Objects The JSP expression language defines a set of implicit objects: pageContext: The context for the JSP page. Provides access to various objects including: servletContext: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context. session: The session object for the client. See Maintaining Client State. request: The request triggering the execution of the JSP page. See Getting Information from Requests. response: The response returned by the JSP page. See Constructing Responses. In addition, several implicit objects are available that allow easy access to the following objects: param: Maps a request parameter name to a single value paramValues: Maps a request parameter name to an array of values header: Maps a request header name to a single value headerValues: Maps a request header name to an array of values cookie: Maps a cookie name to a single cookie initParam: Maps a context initialization parameter name to a single value Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects. pageScope: Maps page-scoped variable names to their values requestScope: Maps request-scoped variable names to their values sessionScope: Maps session-scoped variable names to their values applicationScope: Maps application-scoped variable names to their values
有趣的部分是粗体:)
所以,为了回答你的问题,你应该能够像这样访问它(使用EL):
${param.accountID}
或者,使用JSP scriptlet(不推荐):
<%
String accountId = request.getParameter("accountID");
%>
如果我可以在这里补充评论的话……
< c: value = " $ {param.accountID} " > < / c: >
对我不起作用(它输出0)。
相反,这是可行的:
< c: value = " ${参数[' accountID ']} " > < / c: >
示例:您想删除带有subject_id的主题记录
@RequestMapping(value="subject_setup/delete/{subjectid}",method = RequestMethod.GET)
public ModelAndView delete(@PathVariable int subjectid) {
subjectsDao.delete(subjectid);
return new ModelAndView("redirect:/subject_setup");
}
该参数将用于查询的输入
public int delete(int subjectid) {
String sql = "update tbl_subject set isdeleted= '1' where id = "+subjectid+"";
return template.update(sql);
}
www.somesite.com/Transaction_List.jsp ? accountID = 5
对于这个URL,有一个方法调用请求。java中的getParameter,如果你想在这里将一个数字转换为int,类似于将字符串值转换为string。所以根据你的要求,只需要复制到下一页的下面一行,
int accountId =(int)request.getParameter("accountID");
您现在可以在整个页面中使用accountId调用这个值。
这里accountId是参数的名称,你也可以使用它得到多个参数,但这是行不通的。它将只与GET方法工作,如果你点击POST请求,那么他们将是一个错误。
希望这对你有帮助。
第一页: 细节 第二页: <% String id = request.getParameter("userid") //现在您可以使用id查询HSQL详细产品