使用OGNL通过请求对象访问bean属性

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 22:12   11   0

action这个类里面,要有表单上对应的属性

package roseindia.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import roseindia.model.StudentDetails;
import com.opensymphony.xwork2.ActionSupport;

public class OGNLAction extends ActionSupport implements ServletRequestAware {

 private HttpServletRequest request;
 private String studentname;
 private int age;

 public String getStudentname() {
  return studentname;
 }

 public void setStudentname(String studentname) {
  this.studentname = studentname;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public void setServletRequest(HttpServletRequest request) {
  this.request = request;

 }

 public HttpServletRequest getServletRequest() {
  return request;
 }

 public String execute() throws Exception {

  if (studentname.equals("none")) {
   this.addActionError("Please Select name...");
   return ERROR;
  }
  StudentDetails bean = new StudentDetails();
  bean.setStudentname(studentname);
  bean.setAge(age);
  request.setAttribute("bean", bean);
  return SUCCESS;
 }

}
index.jsp

<%@page language="java" session="true" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Example_OGNL_Struts2</title>
</head>
<body>
 
 <table>
  <tr>
   <td align="center" valign="top" bgcolor="#CCCCFF"><br>Select
    Student Name and Age.... <s:actionerror /> <s:form
     action="ognlAction">
     <s:select list="{'Bharat','Gyan','Ankit','Vinay'}" label="Name"
      headerKey="none" headerValue="None" name="studentname" />
     <s:textfield label="Age" name="age"></s:textfield>
     <s:submit></s:submit>
    </s:form></td>
   <td align="center" valign="top">|Accessing Bean Properties |
    <table cellpadding="2" cellspacing="0" width="200">
     <tr bgcolor="#CC66FF">
      <td>Student name</td>
      <td>Age</td>
     </tr>
     <tr>
      <td><s:property value="studentname" /></td>
      <td><s:property value="age" /></td>
     </tr>
    </table>
   </td>
   <td align="center" valign="top">|Accessing Request |
    <table cellpadding="2" cellspacing="0" width="200">
     <tr bgcolor="#CC66FF">
      <td>Student Name</td>
      <td>Age</td>
     </tr>
     <tr>
      <td><s:property value="#request['bean'].studentname" /></td>
      <td><s:property value="#request['bean'].age" /></td>
     </tr>
    </table>
   </td>
  </tr>
 </table>
</body>
</html>

<struts>
 <constant name="struts.devMode" value="false" />
 <package name="roseindia" extends="struts-default">
  <action name="ognlAction" class="roseindia.action.OGNLAction">
   <result name="error">index.jsp</result>
   <result name="success">index.jsp</result>
   <result name="input">index.jsp</result>
  </action>
 </package>
</struts>
原文:http://www.roseindia.net/struts/struts/struts2.2.1/tags/requestValue.html
源代码:http://pan.baidu.com/share/link?shareid=252734836&uk=3878681452

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP