Struts2数据处理的三种方式(ioc思想)

ioc思想(控制反转,注入),数据不需要手动获取,类型也不需要手动转换(数字直接给你转成int,日期符合格式会给你转成日期格式...)。

一、属性驱动:

使用 struts2获取表单数据:只需表单域名称和 Action处理类的属性名称一致,并且提供属性的set方法,那么在 Action处理类中即可获得表单数据。

登录页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="login.action" method="post">
		username:<input type="text" name="username"><br>
		password:<input type="text" name="password"><br>
		<input type="submit" value="login">
	</form> 
</body>
</html>

 struts.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" extends="struts-default" namespace="/">
		<!-- 模型驱动 -->
		<action name="login" class="cn.sxt.action.LoginAction" method="login">
			<result>/sucess.jsp</result>
		</action>
	</package>
</struts>

Action处理类

/**
 * 属性驱动
 */
import com.opensymphony.xwork2.Action;
public class LoginAction {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public String getPassword() {
		return password;
	}
	public void setUsername(String username) {//一定要有
		this.username = username;
	}
	public void setPassword(String password) {//一定要有
		this.password = password;
	}
	//登录处理业务
	public String login() {
		System.out.println("username="+username+"\tpassword="+password);
		return Action.SUCCESS;
	}
}

如果数据需要显示到页面上,那么该数据可以做为处理类的属性,处理方法后该属性有值,并且有该属性的get方法。那么在页面上可以直接通过el表达式获取。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${username}:${password}登录成功</h1>
</body>
</html>

 属性驱动用的比较少,属性驱动命名要注意。

 二、对象驱动:

在Action的处理类中,属性以对象方式存在,该属性对象只需声明即可,需要保证该属性对象有无参构造方法,并且提供get/set方法。在表单域中的表单域名称以 属性对象名.属性对象的属性来命名。

登录页面login4.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="login4.action" method="post">
		username:<input type="text" name="user.username"><br><!--属性对象名.属性对象的属性-->
		password:<input type="text" name="user.password"><br><!--属性对象名.属性对象的属性-->
		<input type="submit" value="login">
	</form> 
</body>
</html>

 struts.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" extends="struts-default" namespace="/">
		<!-- 对象驱动 -->
		<action name="login4" class="cn.sxt.action.LoginAction4" method="login">
			<result>/sucess4.jsp</result>
		</action>
	</package>
</struts>

Action处理类

import com.opensymphony.xwork2.Action;
import cn.sxt.vo.User;
/**
 * 对象驱动
 */
public class LoginAction4 {
	//保证对象要有无参的构造方法
	private User user;//在 Action的处理类中,属性以对象方式存在,该属性对象只需声明可
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	//登录业务
	public String login() {
		System.out.println("username="+user.getUsername()+":password="+user.getPassword());
		return Action.SUCCESS;
	}
}

显示数据的页面 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>username:${user.username}</h1>
<h1>password:${user.password}</h1>
</body>
</html>

第二种方式用的比较多。 

  三、模型驱动:

在对象驱动中,页面的表单域名称比较的复杂,如果对象属性比较多的情况下,代码量较大。通过模型驱动可以解决这个问题。(我就可以不用向上面对象驱动一样属性对象名.属性对象的属性  直接用属性),模型驱动需要实现 ModelDriven接口,并且主动将对象创建好。

处理类

1、手动创建对象。

2、实现ModelDriven<> 接口  ModelDriven<要注入的对象>。

3、getModel()方法返回创建的对象。

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
import cn.sxt.vo.User;
/**
 * 模型驱动
 * 在对象驱动中,页面的表单域名称比较的复杂,如果对象属性比较多的情况下,代码量比较大。通过模型驱动可以解决这个问题。
 */
//com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor
public class LoginAction implements ModelDriven<User>{//需要实现ModelDriven接口
	
	private User user = new User();//对象需要自己去创建
	public User getUser() {
		return user;//return user = new User();//对象需要自己去创建
	}
	public void setUser(User user) {
		this.user = user;
	}
	//登录业务
	public String login() {
		System.out.println("username="+user.getUsername()+":password="+user.getPassword());
		return Action.SUCCESS;
	}
	@Override
	public User getModel() {
		
		return user;
	}
	//  实现方式  拦截器
	/* com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor
	   public String intercept(ActionInvocation invocation) throws Exception {
	        Object action = invocation.getAction();

	        if (action instanceof ModelDriven) {
	            ModelDriven modelDriven = (ModelDriven) action;
	            ValueStack stack = invocation.getStack();
	            Object model = modelDriven.getModel();  //返回我手动创建的对象
	            if (model !=  null) { //不为空我才加入
	            	stack.push(model);
	            }
	            if (refreshModelBeforeResult) {
	                invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
	            }
	        }
	        return invocation.invoke();
	    }*/

}

 struts.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>
	<!--extends必须写,直接或者间接继承struts-default name自定义 -->
	<package name="default" extends="struts-default" namespace="/">
		<!-- 模型驱动 -->
		<action name="login" class="cn.sxt.action.LoginAction" method="login">
			<result>/sucess.jsp</result>
		</action>
	</package>
</struts>

登录页面login.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="login.action" method="post">
		username:<input type="text" name="username"><br><!--不需要 user. 了  -->
		password:<input type="text" name="password"><br>
		<input type="submit" value="login">
	</form> 
</body>
</html>

显示页面sucess.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>username:${username}</h1>
<h1>password:${password}</h1>
</body>
</

在 ModelDriven里面他要去判断对象是否为空,所以去们需要手动创建。

猜你喜欢

转载自blog.csdn.net/qq_40794973/article/details/85224191
今日推荐