有了前几篇的基础,相信大家对于Struts2已经有了一个很不错的认识,本篇我将为大家介绍一些关于Action接收参数的三种形式,以及简单的表单验证实现,下面进入正题,首先我们一起先来了解一下最基本的Action接收参数的形式,直接在我们的Action类中添加成员变量,这样就可以完成Action接收参数的操作,具体内容详见代码展示:
这里我们以登录验证为例,进行代码展示,首先是我们的Action类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/***
* 最基本的接收参数形式
* @author jho
* http://localhost:8080/Struts3/index!login?name="123"&password="456"
*/
@SuppressWarnings ( "serial" )
public class User extends ActionSupport {
private String name;
private int pwd;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public int getPwd() {
return pwd;
}
public void setPwd( int pwd) {
this .pwd = pwd;
}
public String login(){
System.out.println( "name=" +name+ " Pwd=" +pwd);
if ( 123456 ==pwd){
return SUCCESS;
} else {
return ERROR;
}
}
}
|
下面我们一起来看一下Action的第二种接收参数的形式,通过User类对象来完成整个的参数接收,首先我们创建一个User类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
}
|
然后通过User对象在Action中进行用户提交参数的接收:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/***
* Dormain Model
* @author jho
* http://localhost:8080/Struts4/index!login?user.name="123"&user.password="456"
*/
@SuppressWarnings ( "serial" )
public class UserAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this .user = user;
}
public String login(){
System.out.println( "name=" +user.getName()+ " Pwd=" +user.getPassword());
return SUCCESS;
}
}
|
最后是通过Struts的ModelDriven<User>来进行用户参数的接收,第一步创建我的User类对象,第二部配置我们的Action:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/***
* Model Driven
* @author jho
* http://localhost:8080/Struts5/index!login?name="123"&password="456"
*/
@SuppressWarnings ( "serial" )
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String login(){
System.out.println( "name=" +user.getName()+ " Pwd=" +user.getPassword());
return SUCCESS;
}
public User getModel() {
return user;
}
}
|
所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象。
对于简单的表单验证知识,就为大家介绍一下addFieldError()方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@SuppressWarnings ( "serial" )
public class InputErrorFilter extends ActionSupport{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
public String login(){
System.out.println(name+ "" );
if (name!= null && name.equals( "hpu" )){
return SUCCESS;
} else {
this .addFieldError( "nameError" , "用户名输入有误" );
this .addFieldError( "nameError" , "ul li形式输出错误信息" );
return ERROR;
}
}
}
|
我们的错误处理页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<%@ page language= "java" import = "java.util.*" pageEncoding= "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
<% @taglib uri= "/struts-tags" prefix= "s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
<head>
<base href= "<%=basePath%>" rel= "external nofollow" >
<title>My JSP 'error.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" rel= "external nofollow" >
-->
</head>
<body>
操作失败 <br>
输入参数:<s:text name= "name" /><br/>
<s:fielderror fieldName= "nameError" theme= "simple" /><br/>
<s:property value= "errors.nameError[0]" /><br/>
<s:debug></s:debug>
</body>
</html>
|
以上所述是小编给大家介绍的Struts2中Action三种接收参数形式与简单的表单验证功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/AndroidJotting/p/6516752.html