报错 struts2 可以用ognl拿到值而不可以用el拿到值的解决方法
错误debug后
得到了There is no read method for container的错误
于是我new了一个实体类
1 package com.unity; 2 3 public class Student { 4 private String studentName; 5 6 public Student(String studentName) { 7 this.studentName = studentName; 8 } 9 10 public String getStudentName() { 11 return studentName; 12 } 13 // 14 public void setStudentName(String studentName) { 15 this.studentName = studentName; 16 } 17 }
并在action导入
1 package com.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 import com.opensymphony.xwork2.util.ValueStack; 6 import com.unity.Student; 7 8 public class HelloWorldAction extends ActionSupport { 9 /** 10 * 11 */ 12 private static final long serialVersionUID = -7840227232203094654L; 13 private String studentName; 14 15 public String getStudentName() { 16 return studentName; 17 } 18 19 public void setStudentName(String studentName) { 20 this.studentName = studentName; 21 } 22 23 private String password; 24 25 public String execute() { 26 System.out.println("execute....."); 27 System.out.println("studentName:" + studentName); 28 System.out.println("password:" + password); 29 // ValueStack vs=ActionContext.getContext().getValueStack(); 30 ActionContext context = ActionContext.getContext(); 31 ValueStack vs = context.getValueStack(); 32 // 值栈的栈顶 33 34 Student student = new Student("111111"); 35 36 vs.push(student); 37 return "info"; 38 } 39 40 public String getPassword() { 41 return password; 42 } 43 44 public void setPassword(String password) { 45 this.password = password; 46 } 47 }
虽然没有了There is no read method for container的错误
但是依然无法用el得到值
所以debug可以去死了
后来直接FQ啃英文的狗屎
发现有一个版本的structs2
默认关了页面的el表达式
强制你使用ognl.........
最终解决方法
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" isELIgnored="false"%>
//isELIgnored 是否无视el表达式 重新设置为false
但是我后来发现第二个解决方法
同一版本的structs2在不使用maven的情况下
倒没有出现这样的问题
最后总结
为了规范,哥们在maven的情况下,用struts2就用ogln吧,硬是用el加上那句isELIgnored="false"就行了 ,
最后放点取值的方法
1 <br> 密码:${password } 2 <br> 姓名1:<%=request.getParameter("studentName")%> 3 <br> 姓名2:${studentName} 4 <br> 姓名3: 5 <s:property value="[0].studentName" /> 6 <br> 姓名4: 7 <s:property value="[1].studentName" /> 8 <br> 姓名5:${requestScope.studentName}
1 public class HelloWorldAction extends ActionSupport { 2 /** 3 * 4 */ 5 private static final long serialVersionUID = -7840227232203094654L; 6 private String studentName; 7 8 public String getStudentName() { 9 return studentName; 10 } 11 12 public void setStudentName(String studentName) { 13 this.studentName = studentName; 14 } 15 16 private String password; 17 18 public String execute() { 19 System.out.println("execute....."); 20 System.out.println("studentName:" + studentName); 21 System.out.println("password:" + password); 22 // ValueStack vs=ActionContext.getContext().getValueStack(); 23 ActionContext context = ActionContext.getContext(); 24 ValueStack vs = context.getValueStack(); 25 // 值栈的栈顶 26 Student student = new Student("111111"); 27 vs.push(student); 28 return "info"; 29 } 30 31 public String getPassword() { 32 return password; 33 } 34 35 public void setPassword(String password) { 36 this.password = password; 37 } 38 }
1 public class Student { 2 private String studentName; 3 4 public Student(String studentName) { 5 this.studentName = studentName; 6 } 7 8 public String getStudentName() { 9 return studentName; 10 } 11 // 12 public void setStudentName(String studentName) { 13 this.studentName = studentName; 14 } 15 }
图片