---
title: "对struts2中OGNL数据处理的一点理解"
author: "xiaoliang330"
date: "2011-03-02T15:04:50Z"
tags: ["JSP", "Java", "MVC"]
categories: ["编程语言"]
slug: "struts2-ognl"
draft: false
lastmod: "2011-03-07T18:52:10Z"
id: 938299
url: "https://xiaoliang330.iteye.com/blog/938299"
---

在MVC模式的view层中，jsp中的form表单中元素如input等，都使用OGNL的表达式作为name的值。而在form提交时，这些值都会被设置到Action中的java对象中。而当Action转向到JSP时，struts2的Tag又可以从Action的java对象中，通过OGNL进行取值。 \[code="java"]

user name: department name:

\[/code] \[code="java"]public class OgnlAction extends ActionSupport { private static final Log logger = LogFactory.getLog(OgnlAction.class); private User user; private Department department; /* (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#execute() \*/ @Override public String execute() throws Exception { logger.info("user name:" + user.getName()); // -&gt; downpour logger.info("department name:" + department.getName()); // -&gt; dev return super.execute(); } // setter and getters } \[/code] \[code="java"] user name: department name: \[/code]