jsp页面跟通常的html传值一样 通常有两种方式
1.form表单传值
2.url方式传值
通常后台接受前端传值 都是用的 request.getParameter("username")
但是在struts框架下 可以用set方法让action自动接收 也更方便
所以 action接收值也有两种方式
1.request.getParameter("username")
2.set
下面记录多种组合方式,大家可以根据相应的情况选择使用:
<%@taglib prefix="s" uri="/struts-tags" %>
<form action="getIp/login" method="post" name="form1"> 用户名: <s:textfield name="username" /> <br /> 密 码: <s:password name="password" /> <br /> <s:submit value="提交" /> </form>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>login page</title> </head> <body> <form action="getIp/login" method="post" name="form1"> 用户名: <s:textfield name="username" /> <br /> 密 码: <s:password name="password" /> <br /> <s:submit value="提交" /> </form> </body> </html>
package action; import com.opensymphony.xwork2.ActionSupport; public class loginAction extends ActionSupport { /** * 登录 */ private static final long serialVersionUID = -6797327769546503535L; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() { System.out.println("username:" + username); System.out.println("password:" + password); return SUCCESS; } }
<?xml version="1.0" encoding="utf-8"?> <!-- 指定Spring配置文件的Schema信息 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- <bean id="testAction" class="action.TestAction"> </bean> --> <bean id="login" class="action.loginAction"> </bean> </beans>
action.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="getIp" extends="json-default" namespace="/getIp"> <!-- <action name="testJson" class="testAction"> <result type="json"></result> </action> <action name="testJsp" class="testAction"> <result name="success">/index.jsp</result> </action> --> <action name="login" class="login"> <result type="json"></result> </action> </package> </struts>结果
成功传值
<a href="getIp/login?username=123&password=123">点击传值</a>
其它部分与方式一一样
完整jsp页面代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>login page</title> </head> <body> <form action="getIp/login" method="post" name="form1"> 用户名: <s:textfield name="username" /> <br /> 密 码: <s:password name="password" /> <br /> <s:submit value="提交" /> </form> <a href="getIp/login?username=123&password=123">点击传值</a> </body> </html>
添加jquery框架
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script> $(function() { <span style="white-space:pre"> </span>bindLogin(); }); function bindLogin() { <span style="white-space:pre"> </span>$("#btnSubmit").bind("click", <span style="white-space:pre"> </span>function() {<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>var txtUserName = $("#username3").val(); <span style="white-space:pre"> </span>var txtPwd = $("#password3").val(); <span style="white-space:pre"> </span>if (! (txtUserName && txtPwd)) { <span style="white-space:pre"> </span>alert("请输入帐号和密码;"); <span style="white-space:pre"> </span>return; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>$.getJSON("<%=basePath%>getIp/login?username=" + txtUserName + "&password=" + txtPwd, <span style="white-space:pre"> </span>function(data) {<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>alert(data);<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>if (data.aaa == "fail") { <span style="white-space:pre"> </span>alert("很抱歉,用户名不存在或者密码错误。---请确认密码"); <span style="white-space:pre"> </span>}else { <span style="white-space:pre"> </span>window.location.href = "<%=basePath%>index.jsp"; <span style="white-space:pre"> </span>sessionValuesTemp = data.userId; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>}); } <span style="white-space:pre"> </span></script>
<div class="login_box_wrapper"> <span style="white-space:pre"> </span><div class="login_box"> <span style="white-space:pre"> </span><ul> <span style="white-space:pre"> </span><li class="devideLine"><span>用户名</span><input type="text" class="userName" id="username3" /></li> <span style="white-space:pre"> </span><li><span>密码</span><input type="password" class="passWord" id="password3" /></li> <span style="white-space:pre"> </span></ul> <span style="white-space:pre"> </span><button type="button" id="btnSubmit">方式三提交</button> <span style="white-space:pre"> </span></div> <span style="white-space:pre"> </span></div>
其它部分与方式一一样
完整jsp页面代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>login page</title> <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script> <script> $(function() { bindLogin(); }); function bindLogin() { $("#btnSubmit").bind("click", function() { var txtUserName = $("#username3").val(); var txtPwd = $("#password3").val(); if (! (txtUserName && txtPwd)) { alert("请输入帐号和密码;"); return; } $.getJSON("<%=basePath%>getIp/login?username=" + txtUserName + "&password=" + txtPwd, function(data) { alert(data); if (data.aaa == "fail") { alert("很抱歉,用户名不存在或者密码错误。---请确认密码"); }else { window.location.href = "<%=basePath%>index.jsp"; sessionValuesTemp = data.userId; } }); }); } </script> </head> <body> <form action="getIp/login" method="post" name="form1"> 用户名: <s:textfield name="username" /> <br /> 密 码: <s:password name="password" /> <br /> <s:submit value="提交" /> </form> <a href="getIp/login?username=123&password=123">点击传值</a> <div class="login_box_wrapper"> <div class="login_box"> <ul> <li class="devideLine"><span>用户名</span><input type="text" class="userName" id="username3" /></li> <li><span>密码</span><input type="password" class="passWord" id="password3" /></li> </ul> <button type="button" id="btnSubmit">方式三提交</button> </div> </div> </body> </html>
例如我们把用户名密码封装成用户 User
User.java
package entity; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
public class loginAction extends ActionSupport{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user=user; } }jsp页面中
<form action="login" method="post"name="form1"> 用户名:<s:textfield name="user.username"/><br/> 密 码:<s:password name="user.password"/><br/> <s:submit value="提交"/> </form>
Action类必须实现ModelDriven接口,同样把表单传来的数据封装起来,Action类中必须实例化该对象,并且要重写getModel()方法
public class loginAction extends ActionSupport implements ModelDriven<User>{ private User user =new User(); public User getModel(){ return user; } }