新手在学struts2,出现了
freemarker.core.InvalidReferenceException: Expression parameters.id is undefined on line 66, column 8 in template/simple/radiomap.ftl.异常,但是我根本就没有有使用过id参数,请问怎么解决啊?
Action动作类:
1 package cn.itcast.ognl; 2 3 import java.util.Map; 4 5 import javax.servlet.ServletContext; 6 import javax.servlet.http.HttpServletRequest; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 import com.opensymphony.xwork2.util.ValueStack; 12 13 @SuppressWarnings("serial") 14 public class OgnlAction extends ActionSupport { 15 16 private String name = "oname"; 17 18 private String sex = "male"; 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public String getSex() { 29 return sex; 30 } 31 32 public void setSex(String sex) { 33 this.sex = sex; 34 } 35 36 public String test(){ 37 System.out.println("OgnlAction ************ test()"); 38 39 HttpServletRequest request = ServletActionContext.getRequest(); 40 request.setAttribute("username", "username_request"); 41 request.setAttribute("password", "password_request"); 42 43 Map sessionMap = ServletActionContext.getContext().getSession(); 44 sessionMap.put("username", "username_session"); 45 sessionMap.put("password", "password_session"); 46 47 ServletContext sc = ServletActionContext.getServletContext(); 48 sc.setAttribute("username", "username_application"); 49 sc.setAttribute("password", "password_application"); 50 51 //方法一:获取ValueStack对象 52 ValueStack valueStack1 = (ValueStack)request.getAttribute("struts.valueStack"); 53 System.out.println("valueStack = "+valueStack1); 54 55 /* 56 * 插入的内容,实际上是先放置在一个map集合中,又把这个map集合放置在对象栈里 57 */ 58 valueStack1.set("error", "error_valueStack"); 59 valueStack1.set("msg", "msg_valueStack"); 60 61 valueStack1.getRoot().add(0, new Person()); 62 valueStack1.getRoot().add(0,new Employee()); 63 64 //方法二:获取ValueStack对象 65 /*ValueStack valueStack2 = ServletActionContext.getContext().getValueStack(); 66 System.out.println("valueStack = "+valueStack2);*/ 67 68 69 return "ognl"; 70 } 71 72 }
要跳转到的jsp页面:
1 <%@ page language="java" pageEncoding="utf-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <html> 4 <head> 5 <title>My JSP 'index.jsp' starting page</title> 6 <meta http-equiv="description" content="This is my page"> 7 </head> 8 <body> 9 使用EL表达式取值-----------------------------------------<br> 10 ${requestScope.username}<br> 11 ${sessionScope.username}<br> 12 ${applicationScope.username}<br><br><br><br> 13 使用Ognl表达式取值-----------------------------------------<br> 14 1 # 如果访问其他Context中的对象,由于他们不是根对象,所以在访问时,需要添加#前缀。<br> 15 <s:property value="#request.username"/><br> 16 <s:property value="#session.username"/><br> 17 <s:property value="#application.username"/><br><br> 18 <s:property value="#request['username']"/><br> 19 <s:property value="#parameters.cid[0]"/><br> 20 <s:property value="#attr.username"/><br><br> 21 2 # 如果要访问根对象(即ValueStack)中对象的属性,则可以省略#命名对象,直接访问该对象的属性即可。<br> 22 <s:property value="msg"/><br><br> 23 深入理解值栈中的 ObjectStack<br> 24 <s:property value="name"/><br> 25 <s:property value="sex"/><br> 26 <s:property value="age"/><br> 27 <s:property value="salary"/><br><br> 28 29 30 用法3:构造Map<br> 31 <s:radio list="#{'01':'男','02':'女'}"></s:radio><br><br><br><br> 32 %的用法:“%”符号的用途是在标签的属性值被理解为字符串类型时,告诉执行环境%{}里的是OGNL表达式。 <br> 33 <s:property value="#request.username"/><br> 34 <s:property value="%{#request.username}"/>%{}是万能用法,无论里面的表达式是不是ognl表达式,都会强制理解为ognl表达式<br><br> 35 36 “$”有两个主要的用途<br> 37 * 用于在国际化资源文件中,引用OGNL表达式<br> 38 <s:text name="ognl" /><br><br> 39 40 * 在Struts 2配置文件中,引用OGNL表达式<br> 41 <s:property value="#parameters.msgxx[0]"/><br><br> 42 43 <s:debug></s:debug> 44 </body> 45 </html>
起始页test.jsp:
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> 测试值栈:<br> <a href="${pageContext.request.contextPath}/ognl/ValueStackAction_test.action">test</a><br> <br> <br> <br> 测试ognl表达式的用法<br> <a href="${pageContext.request.contextPath}/ognl/OgnlAction_test.action?cid=9">test</a><br> 测试标签<br> <a href="${pageContext.request.contextPath}/ognl/OgnlTagAction_test.action?cid=9">test</a><br> </body> </html>
要跳转的jsp页面31行改为:
<s:radio list="#{'01':'男','02':'女'}" name="sex" label="性别"/><br><br>