El表达式取不到值
- Java code
package com.rec.action;import java.util.List;import javax.annotation.Resource;import com.opensymphony.xwork2.ActionSupport;import com.rec.model.Admin;import com.rec.model.User;import com.rec.service.UserService;@SuppressWarnings("serial")public class UserAction extends ActionSupport { private User user; private UserService userService; private String name; private List<User> userList; private String password; public User getUser() { return user; } // @Autowired public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login() { Admin admin=userService.login(name, password); user=userService.findById(admin.getUser_id()); if (admin != null&&user!=null) { return "SUCCESS"; } else return "ERROR"; } public String findAllUser(){ userList=userService.findAll(); if(userList!=null) return "SUCCESS"; else return "ERROR"; } public String delete(){ this.userService.delete(this.user); return "success"; } public List<User> getUserlist() { return userList; } public void setUserlist(List<User> userlist) { this.userList = userlist; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; }}
- HTML code
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'User.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --><style>.tableborder { background: #4D76B3; border: 0px solid #4D76B3 } .altbg1 { background: #F3F6FA }.altbg2 { background: #FFFFFF }.STYLE5 {font-size: 12px; color: #FFFFFF; font-weight: bold; }.STYLE7 {font-size: 12px; color: #FFFFFF; font-weight: bold; font-family: Arial, Helvetica, sans-serif; }.STYLE8 { font-size: 11px; font-weight: bold;}.STYLE10 { font-size: 12px; color: #FFFFFF;}.STYLE12 {font-size: 12px}.STYLE14 {font-size: 12px; font-weight: bold; }.STYLE15 {font-weight: bold}</style> </head> <body> <table width="765" height="710" align="center" background="images/userInfo.jpg"> <tr> <td> <div> <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000000"> <tr> <td height="24" colspan="4" align="center" bgcolor="#FFFFFF"><br></td> </tr> <tr> <td height="24" bgcolor="#FFFFFF"><span class="STYLE6">用户姓名:</span></td> <td height="24" bgcolor="#FFFFFF"><span class="STYLE6"><s:property value="#user.getName()"/></span></td> <td height="24" bgcolor="#FFFFFF"><span class="STYLE6">用户性别:</span></td> <td height="24" bgcolor="#FFFFFF"><span class="STYLE6">空</span></td> </tr> <tr> <td height="24" bgcolor="#FFFFFF"><span class="STYLE6"><br></span></td> <td height="24" bgcolor="#FFFFFF"><span class="STYLE6">入职日期</span></td> <td height="24" bgcolor="#FFFFFF"><span class="STYLE6">空</span></td> </tr> <tr> <td height="24" colspan="3" bgcolor="#FFFFFF"><span class="STYLE6">a</span></td> </tr> </table><br> <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000000"> <tr> <td height="24" colspan="4" align="center" bgcolor="#FFFFFF" class="STYLE6">所在部门信息</td> </tr> <tr> <td height="24" bgcolor="#FFFFFF" class="STYLE6">部门名称:</td> <td height="24" bgcolor="#FFFFFF" class="STYLE6">a</td> <td height="24" bgcolor="#FFFFFF" class="STYLE6">建立时间:</td> <td height="24" bgcolor="#FFFFFF" class="STYLE6">未知</td> </tr> <tr> <td height="24" bgcolor="#FFFFFF" class="STYLE6">部门介绍:</td> <td height="24" colspan="3" bgcolor="#FFFFFF" class="STYLE6">a</td> </tr> </table><br> <center><a href="index.html">退出</a></center> </div> </td> </tr> </table> </body></html>
[解决办法]
><s:property value="#user.getName()"/>
改成
<s:property value="user.name"/>
[解决办法]
<td height="24" bgcolor="#FFFFFF"><span class="STYLE6">用户姓名:</span></td>
<td height="24" bgcolor="#FFFFFF"><span class="STYLE6>${user.name}</span></td>
这才是el表达式的写法吧
[解决办法]
将web.xml文件中的内容改一下:
如果有以下内容,表示是Servlet 2.3 / JSP 1.2。
<!--CTYPE web-app PUBLIC </sp-->
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
在默认情况下,Servlet 2.3 / JSP 1.2是不支持EL表达式的,而Servlet 2.4 / JSP 2.0支持。
如果web.xml如下设置也不支持EL表达式:
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
改为:一般情况下version=2.4支持el表达式,而2.5不支持
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">