一个struts2小例子:使用 及javascript中显示变量值的问题

1.struts.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC 
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6.     <package name="hello" extends="struts-default">
  7.         <action name="Hello" class="edu.jlu.hgd.Hello">
  8.             <result name="success">success.jsp</result>
  9.         </action>
  10.     </package>
  11. </struts>
2.action
  1. package edu.jlu.hgd;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.opensymphony.xwork2.ActionSupport;
  5. public class Hello extends ActionSupport{
  6.     /**
  7.      * 
  8.      */
  9.     private static final long serialVersionUID = 1L;
  10.     private List<Student> list=new ArrayList<Student>();
  11.     @Override
  12.     public String execute() throws Exception {
  13.         // TODO Auto-generated method stub
  14.         list.add(new Student("hgd",1));
  15.         list.add(new Student("wjh",2));
  16.         list.add(new Student("lx",3));
  17.         return SUCCESS;
  18.     }
  19.     
  20.     public List<Student> getList() {
  21.         return list;
  22.     }
  23.     public void setList(List<Student> list) {
  24.         this.list = list;
  25.     }
  26. }
3.success.jsp
  1. <%@page contentType="text/html; charset=utf-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags"%>
  3. <html>
  4.     <body>
  5.         <table>
  6.             <s:iterator id="a" value="list">
  7.                 <tr>
  8.                     <td>
  9.                     <s:property value="%{num}"/>
  10.                     <script type="text/javascript">
  11.                     document.write('${a.name}');
  12.                     </script>
  13.                     </td>
  14.                 </tr>
  15.             </s:iterator>
  16.         </table>
  17.     </body>
  18. </html>
4.student.java
  1. package edu.jlu.hgd;
  2. public class Student {
  3.     private String name;
  4.     private int num;
  5.     public String getName() {
  6.         return name;
  7.     }
  8.     public void setName(String name) {
  9.         this.name = name;
  10.     }
  11.     public int getNum() {
  12.         return num;
  13.     }
  14.     public void setNum(int num) {
  15.         this.num = num;
  16.     }
  17.     public Student() {
  18.         // TODO Auto-generated constructor stub
  19.     }
  20.     public Student(String name, int num) {
  21.         this.name = name;
  22.         this.num = num;
  23.     }
  24. }


你可能感兴趣的:(JavaScript,list,struts,Class,encoding)