一 接收表单参数的三种方式
二 第一方式——使用Action属性接收参数
1、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">
密码:<input type="password" name="password">
<input type="submit" value="提交" />
</form>
</body>
</html>
2、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" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/error.jsp</result>
</action>
<action name="*_*_*" method="{2}" class="com.cakin.{3}.{1}Action">
<result>/result.jsp</result>
<result name="add">/{2}.jsp</result>
<result name="update">/{2}.jsp</result>
</action>
<action name="LoginAction" method="login" class="com.cakin.action.LoginAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
3、LoginAction.java
package com.cakin.action;
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;
}
}
4、success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 success JSP page. <br>
</body>
</html>
5、测试
三 第二方式——使用Domain Model接收参数
1、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">
密码:<input type="password" name="user.password">
<input type="submit" value="提交" />
</form>
</body>
</html>
2、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" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/error.jsp</result>
</action>
<action name="*_*_*" method="{2}" class="com.cakin.{3}.{1}Action">
<result>/result.jsp</result>
<result name="add">/{2}.jsp</result>
<result name="update">/{2}.jsp</result>
</action>
<action name="LoginAction" method="login" class="com.cakin.action.LoginAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
3、LoginAction.java
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login(){
System.out.println(user.getUsername());
return SUCCESS;
}
}
4、User.java
package com.cakin.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;
}
}
5、测试
四 第三方式——使用ModelDriven接收参数
1、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">
密码:<input type="password" name="password">
<input type="submit" value="提交" />
</form>
</body>
</html>
2、LoginAction.java
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private User user =new User();
public String login(){
System.out.println(user.getUsername());
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
3、测试
五 综合——使用ModelDriven接收列表参数
1、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">
密码:<input type="password" name="password">
书籍1:<input type="text" name="booklist[0]">
书籍2:<input type="text" name="booklist[1]">
<input type="submit" value="提交" />
</form>
</body>
</html>
2、User.java
package com.cakin.po;
import java.util.List;
public class User {
private String username;
private String password;
private List<String> booklist;
public List<String> getBooklist() {
return booklist;
}
public void setBooklist(List<String> 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;
}
}
3、LoginAction.java
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
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));
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
4、测试
六 综合——使用ModelDriven接收对象参数
1、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">
密码:<input type="password" name="password">
书籍1:<input type="text" name="booklist[0].username">
书籍2:<input type="text" name="booklist[1].username">
<input type="submit" value="提交" />
</form>
</body>
</html>
2、User.java
package com.cakin.po;
import java.util.List;
public class User {
private String username;
private String password;
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;
}
}
3、LoginAction.java
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private User user =new User();
public String login(){
System.out.println(user.getUsername());
System.out.println(user.getBooklist().get(0).getUsername());
System.out.println(user.getBooklist().get(1).getUsername());
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
4、测试
相关推荐
Struts2 Struts2核心控制流程、Ognl、Action、Interceptor、Result、FreeMarker、Struts2标记库、Struts2扩展、Struts2应用技巧(输入验证、消息国际化、文件上传和下载、防止重复提交等)。 熟练掌握Struts2核心...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 ...
15.5、深入Struts应用 15.6、本章摘要 15.7、开发实战讲解(基于Oracle数据库) 第16章 Struts常用标签库 16.1、Struts标签库简介 16.2、Bean标签 16.2.1、标签 16.2.2、标签 16.2.3、资源访问标签 16.2.4...
国内知名的高端IT技术作家,已出版《Spring 2.0宝典》、《基于J2EE的Ajax宝典》、《轻量级J2EE企业应用实战》、《Struts 2权威指南》、《Ruby On Rails敏捷开发最佳实践》等著作。 目录: 第0章 学习Java...