三种方式
1、使用Action的属性接收参数
2、使用Domain Model 接收参数
3、使用ModelDriven接收参数
方式一:
java类:
package cn.hb.si;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
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 login(){
System.out.println(userName);
return SUCCESS;
}
}
struts2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<!-- 引入其他的配置文件 可以添加多个,也可以添加包名-->
<include file="hello.xml"></include>
<!-- 配置编码方式 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
</struts>
引用的hello.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="LoginAction" method="login" class="cn.hb.si.LoginAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
success.xml
<%@ 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 'success.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>
This is my success JSP page. <br>
</body>
</html>
login.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 'login.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="LoginAction.action" method="post">
用户名:<input type="text" name="userName"><br>
密码: <input type="password" name="passWord"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
访问地址:http://localhost:8080//Struts2_1zixue/login.jsp
方式二:
java类
package cn.hb.si;
import cn.hb.po.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private User user;
public String login(){
System.out.println(user.getUserName());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
package cn.hb.po;
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;
}
}
login.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 'login.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="LoginAction.action" method="post">
用户名:<input type="text" name="user.userName"><br>
密码: <input type="password" name="user.passWord"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
struts2.xml 和 web.xml和上面相同
访问地址:http://localhost:8080//Struts2_1zixue/login.jsp
方式三:
java类:
package cn.hb.si;
import cn.hb.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
// private User user;
/**
* 如果实现ModelDriven 就不能有get和set方法,同时也必须实例化User对象
* @author Ke_Li
* @return
* 2016年3月9日上午11:17:25
*/
private User user=new User();
public String login(){
System.out.println(user.getUserName());
return SUCCESS;
}
// public User getUser() {
// return user;
// }
//
// public void setUser(User user) {
// this.user = user;
// }
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
user类同上 struts2.xml web.xml hello.xml同上
login.jsp 这次不需要指定那个User用户
<%@ 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 'login.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="LoginAction.action" method="post">
用户名:<input type="text" name="userName"><br>
密码: <input type="password" name="passWord"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
访问地址:http://localhost:8080//Struts2_1zixue/login.jsp
方式三;
package cn.hb.si;
import cn.hb.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
// private User user;
/**
* 如果实现ModelDriven 就不能有get和set方法,同时也必须实例化User对象
* @author Ke_Li
* @return
* 2016年3月9日上午11:17:25
*/
private User user=new User();
public String login(){
System.out.println(user.getUserName());
// 方式一
// System.out.println(user.getBookList().get(0));
// System.out.println(user.getBookList().get(1));
// 方式二
System.out.println(user.getBookList().get(0).getUserName());
System.out.println(user.getBookList().get(1).getUserName());
return SUCCESS;
}
// public User getUser() {
// return user;
// }
//
// public void setUser(User user) {
// this.user = user;
// }
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
package cn.hb.po;
import java.util.List;
public class User {
private String userName;
private String passWord;
// private List<String>bookList;
private List<User>bookList;
public List<User> getBookList() {
return bookList;
}
public void setBookList(List<User> bookList) {
this.bookList = bookList;
}
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;
}
}
login.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 'login.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="LoginAction.action" method="post">
用户名:<input type="text" name="userName"><br>
密码: <input type="password" name="passWord"><br>
<!-- 书籍:<input type="text" name="bookList[0]"><br>
书籍:<input type="text" name="bookList[1]"><br> -->
书籍:<input type="text" name="bookList[0].userName"><br>
书籍:<input type="text" name="bookList[1].userName"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
struts.xml web.xml hello.xml 同上
访问地址:同上