1. 程式人生 > >第22講 struts2-OGNL訪問靜態方法和屬性

第22講 struts2-OGNL訪問靜態方法和屬性

在HeadFirstStruts2chapter04 專案中,
1新建com.cruise.common,Mystatic類,寫靜態方法和靜態屬性,
MyStatic 並沒有繼承ActionSupport
package com.cruise.common;

public class MyStatic {

    public static final String str="www.cruiseloveashley.com";
    public

 static String  printUrl(){
       return "cruiseloveAshley";
    }
}
2success.jsp,
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" 
uri="/struts-tags" %>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
ognl
訪問JavaBean的資料:<s:property value="student.name"/><s:property value="student.age"/><br>
ognl訪問List集合的資料:<s:property value="students[0].name"/><s:property value="students[0].age"/><br>
ognl訪問List集合的資料:<s:property value="students[1].name"/><s:property value="students[1].age"/><br>

ognl訪問Map集合的資料:<s:property value="studentMap['goodStudent'].name"/><s:propertyvalue="studentMap['goodStudent'].age"/><br>
ognl訪問Map集合的資料:<s:property value="studentMap['badStudent'].name"/><s:propertyvalue="studentMap['badStudent'].age"/><br>
ognl訪問靜態屬性的資料:<s:property value="@[email protected]"/><br>
ognl訪問靜態屬性的資料:<s:property value="@[email protected]()"/><br>
body>
html>
3struts.xml 新增配置 ,允許訪問靜態方法,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="manage" namespace="/" extends="struts-default">
    <action name="hello" class="com.cruise.action.HelloAction" >
       <result name="success" >success.jsp</result>
    </action>
</package>
</struts>
HelloAction如下:
package com.cruise.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cruise.model.Student;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{
    
    private Student student;
    private List students;
    private Map studentMap;
    
    public Student getStudent() {
       return student;
    }

    public void setStudent(Student student) {
       this.student = student;
    }
    
    public List getStudents() {
       return students;
    }

    public void setStudents(List students) {
       this.students = students;
    }

    public Map getStudentMap() {
       return studentMap;
    }

    public void setStudentMap(Map studentMap) {
       this.studentMap = studentMap;
    }

    @Override
    public String execute() throws Exception {
       
       student= new Student("小八","21");
       
       students = new ArrayList();
       students.add(new Student("ashely","23"));
       students.add(new Student("jack","45"));
       
       studentMap = new HashMap();
       studentMap.put("goodStudent", new Student("cruise","26"));
       studentMap.put("badStudent", new Student("Tom","23"));
       return "success";
    }
}

4測試:本案例中HelloAction中並沒有太多作用,只不過實現了跳轉頁面。
http://localhost:8080/HeadFirstStruts2chapter04/hello