问题情况:设置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天了,请给个明白的解释吧,谢谢
试试el表达式 ${messageStore.message}
不知道 struts标签是否支持 对象 点 属性
struts2采用的是映射机制,就是说你在action定义的变量通过set和get方法在页面中是可以得到数据的也就是说如果你想要在页面中取得action中的值的话就一定要把它定义出来,并且给他至少一个get方法,你可以吧MessageStore定义到你的action中去然后通过getMessageStore映射到页面上然后再通过struts2的标签提取其中的数据。 在你的MessageStore类的构造方法里随便打印一句话出来,你就明白错在哪了