struts2标签在jsp页面取不到值,新手求学,老手赐教

shan4260 2012-11-06 04:43:48
问题情况:设置struts框架后,建立index.jsp页面,HelloWorldAction.java,MessageStore.java,
我想通过index页面请求action获取MessageStore.java中的message的值然后输出到HelloWorld页面,但是<s:property value="messageStore.message" default="no value"/>后value没获取值。
全部代码和主要设置如下:
index .jsp
<%@page import="com.opensymphony.xwork2.util.*" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!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=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>



HelloWorld.jsp:

<%@page import="com.opensymphony.xwork2.util.*" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!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=ISO-8859-1">
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="messageStore.message" default="no value"/></h2>
<%%>
</body>
</html>



MessageStore.java:


package hellomodel;

public class MessageStore {
private String message;
public MessageStore(){
setMessage("Hello,Struts user!");
}
public void setMessage(String message){
this.message=message;
}
public String getMesssage(){
return message;
}

}


HelloWorldAction.java:


package helloaction;
import hellomodel.MessageStore;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private MessageStore messageStore;
public String excute() throws Exception{
messageStore=new MessageStore();
//this.setMessageStore(msg);
return SUCCESS;
}
public MessageStore getMessageStore() {
return messageStore;
}

public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}
}


struts.config:

<?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.devMode" value="true" />

<package name="basicstruts2" extends="struts-default">

<action name="index">
<result>/index.jsp</result>
</action>
<action name="hello" class="helloaction.HelloWorldAction" method="excute" >
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Basic_Struts2_Ant</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>



其中红色部分是我想获得字符串“Hello,Struts user!"的地方,但是只显示默认的no value,鼓捣3天了,请给个明白的解释吧,谢谢
...全文
1164 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
正皇旗 2014-09-23
  • 打赏
  • 举报
回复
二楼正解!多谢!
yjflinchong 2012-11-07
  • 打赏
  • 举报
回复
引用 5 楼 diypyh 的回复:
引用 1 楼 yjflinchong 的回复:试试el表达式 ${messageStore.message} 不知道 struts标签是否支持 对象 点 属性 struts2里使用了OGNL表达式,比EL更强大,所以EL能做到的,struts2也一样能做到。
struts2 标签。 转移不方便。 还是习惯了el
shan4260 2012-11-07
  • 打赏
  • 举报
回复
真心多谢各位,果然是多了个字母
redlotus_lyn 2012-11-07
  • 打赏
  • 举报
回复
getMesssage()错误!
nicholasbobo 2012-11-06
  • 打赏
  • 举报
回复
你的MessageStore里的getMessage()写成getMesssage()了,这种get,set的方法还是最好用myeclipse自带的功能自动生成为好,免得出错
diypyh 2012-11-06
  • 打赏
  • 举报
回复
引用 1 楼 yjflinchong 的回复:
试试el表达式 ${messageStore.message} 不知道 struts标签是否支持 对象 点 属性
struts2里使用了OGNL表达式,比EL更强大,所以EL能做到的,struts2也一样能做到。
diypyh 2012-11-06
  • 打赏
  • 举报
回复
在HelloWorldAction类中的excute()方法中,去掉这句messageStore=new MessageStore(); Action在初始化时就已经为里面的成员变量实例化好了。 你这样相当于重新创建了一个新对象,然后将这个新对象传到页面了,此对象是没有任何值的。
zswiori 2012-11-06
  • 打赏
  • 举报
回复
在你的MessageStore类的构造方法里随便打印一句话出来,你就明白错在哪了
fatxyn 2012-11-06
  • 打赏
  • 举报
回复
struts2采用的是映射机制,就是说你在action定义的变量通过set和get方法在页面中是可以得到数据的也就是说如果你想要在页面中取得action中的值的话就一定要把它定义出来,并且给他至少一个get方法,你可以吧MessageStore定义到你的action中去然后通过getMessageStore映射到页面上然后再通过struts2的标签提取其中的数据。
yjflinchong 2012-11-06
  • 打赏
  • 举报
回复
试试el表达式 ${messageStore.message} 不知道 struts标签是否支持 对象 点 属性

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧