日期:2014-05-16  浏览次数:20465 次

struts获取jsp页面上的参数

1.通过<form></form>传参

JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="user.action" method="post" >
    	用户名:<input type="text" name="uname" id="uname"/></br>
    	密码:<input type="text" name="uname" id="pwd"/></br>
    	<input type="submit" value="提交"/>
    </form>
  </body>
</html>

?

2.在action类中get,set方法就可以获取页面上的参数

action类:

package com.action;

import java.util.List;

import com.bean.testbean;
import com.opensymphony.xwork2.ActionSupport;

public class testaction extends ActionSupport{
	private String uname;
	private String pwd;
	
	
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public String execute(){
		System.out.println("用户名:========="+uname);
		System.out.println("密码:========"+pwd);
		return "ok";
		
	}
	
}

3.struts.xml的配置

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="default" extends="struts-default" namespace="/">
		 <action name="user" class="com.action.testaction">
			<result name="ok">/index.jsp</result>
		</action>
		
	</package>
</struts>    
?

?