1

I'm newbie and work with Struts 2. I waste some days but cannot fix it.

  • eclipse 4.4.2 luna
  • struts 2.3.20
  • tomcat 8

action class:

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

  • No error
  • Log is ok

It only displays AAAA in <h4> but does not show the message. I try both XML and annotation but it doesn't show the message. I don't know what the problem is with my project.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
ducpm121
  • 11
  • 3

1 Answers1

2
  1. You are returning the constant SUCCESS, that is mapped to the String "success", and then mapping the String "SUCCESS" in the struts.xml.

    Change "SUCCESS" to "success" in struts.xml:

     <result name="success">/index.jsp</result>
    
  2. You are probably opening the page without passing through the action before.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I changed but it wont work. I've tried both EL and Struts tag but it display only AAAA of

    tag. I download "helloworld" project in some struts2 tutorial page but it like my project that not show any value in Action. I dont know what the problem in here.

    – ducpm121 Dec 10 '15 at 16:54
  • As I said, you are calling the jsp in the url, while you should call the action – Andrea Ligios Dec 11 '15 at 08:37
  • Did it work ? Have you called `testdemo/index.action` instead of `testdemo/index.jsp` ? Please remember to accept the answer if it worked – Andrea Ligios Dec 15 '15 at 14:15
  • i tried changed like you and set type in result tag as chain and redirectAction. But all method i try not work. – ducpm121 Dec 16 '15 at 02:35