Struts 2 - 无法在JSP页面中显示Action中的值

时间:2015-12-10 16:16:30

标签: jsp configuration struts2 struts-config

我是新手并且使用Struts 2.我浪费了几天但无法修复它。

  • eclipse 4.4.2 luna
  • struts 2.3.20
  • tomcat 8

动作类:

public class UserAction extends ActionSupport implements Action{

    private static final long serialVersionUID = 3665293407194339009L;
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        message="this is inside execute";
        return SUCCESS;
    }

}

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="html" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="index" class="com.action.controller.UserAction" method="execute">
            <result name="SUCCESS">/index.jsp</result>
        </action>
    </package>
</struts>

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
    </head>
    <body>
        <h4>AAAAA</h4>
        <s:property value="message"/>
    </body>
</html>

this is screen when project run

  • 没有错误
  • 记录正常

它仅在<h4>中显示AAAA,但不显示该消息。我尝试了XML和注释,但它没有显示消息。我不知道我的项目有什么问题。

1 个答案:

答案 0 :(得分:2)

  1. 您将返回映射到字符串SUCCESS的常量"success",然后将字符串"SUCCESS"映射到struts.xml中。

    struts.xml 中的"SUCCESS"更改为"success"

    <result name="success">/index.jsp</result>
    
  2. 您可能在未经过此操作之前打开该页面。