當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式
生活随笔
收集整理的這篇文章主要介紹了
struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文演示了JSP中獲取HTTP參數的幾種方式,還有action中獲取HTTP參數的幾種方式。
1. 創建JSP頁面(testParam.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page isELIgnored="false"%> <%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head></head><body>JSP頁面中三種獲取HTTP Parameter的方法:<br/>1.<s:property value="#parameters.username"/><br/>2.<s:property value="#parameters['remark']"/><br/>3.<%=request.getParameter("username")%><br/><s:form action="testParam" method="post">用戶名:<s:textfield name="username"></s:textfield><br/>備注:<s:textfield name="remark"></s:textfield><br/><s:submit value="提交"></s:submit><br/></s:form></body> </html>2. 創建Action類(ParameterTestAction.java)
package com.clzhang.struts2.demo4;import java.util.*; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ActionContext; import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest;public class ParameterTestAction extends ActionSupport {public static final long serialVersionUID = 1;private String username;private String remark;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public String execute() throws Exception {// 方式一:將參數設置為Action的屬性,OGNL會自動填充它System.out.println("1.把參數作為Action的屬性...");System.out.println("username=" + this.username);System.out.println("remark=" + this.remark);// 方法二:在Action中使用ActionContext得到包含parameter的Map以獲取參數ActionContext context = ActionContext.getContext();Map parameterMap = context.getParameters();String usernameArray[] = (String[]) parameterMap.get("username");String remarkArray[] = (String[]) parameterMap.get("remark");System.out.println("2.通過ActionContext.getContext().getParameters()取得...");System.out.println("username: " + usernameArray[0]);System.out.println("remark: " + remarkArray[0]);// 方法三:在Action中取得HttpServletRequest對象,常規方法取得HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);String username = request.getParameter("username");String remark = request.getParameter("remark");System.out.println("3.通過HttpServletRequest對象取得...");System.out.println("username: " + username);System.out.println("remark: " + remark);return SUCCESS;} }3. 修改struts.xml文件
<action name="testParam" class="com.clzhang.struts2.demo4.ParameterTestAction"><result name="success">/struts2/demo4/testParam.jsp</result></action>4. 測試
打開IE,輸入如下地址:http://127.0.0.1:8080/st/struts2/demo4/testParam.jsp?username=zhang1&remark=that'sok
注意URL后面的參數。
結果如下:
輸入用戶名:張三,備注:不錯的狀態,提交,結果如下:
后臺輸出如下:
總結
以上是生活随笔為你收集整理的struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 系统字体
- 下一篇: 基于Python的DBC转Excel脚本