-->

Unable to get Struts2 Hello World to work using Ec

2019-01-09 17:40发布

问题:

This site doesn't allow user to ask technical questions to their tutorial which I believe is broken:

http://www.mkyong.com/misc/how-to-use-mkyong-tutorial/

I'm using:

  • maven: 3.2.1
  • tomcat 7
  • java 1.7
  • Eclipse: Luna Release (4.4.0)

I'm getting this message:

WARNING: Could not find action or result
There is no Action mapped for namespace [/] and action name [] associated with context path [/Struts2Example]. - [unknown location]

I can never get to the login.jsp page unless I enter the full url. But even then, when I click on the submit button, it doesn't go to the welcome_user.jsp page either.

Can someone tell me how I can fix this and get this Hello World example to work in using Eclipse?

回答1:

If you follow the tutorial, which is linked to the page Struts 2 Hello World Example, and done everything till p. 7 then you should Run it as is written

In Struts2, you can access the action class directly with a suffix of .action.

http://localhost:8080/Struts2Example/User/Login.action

If you tried to access application as

http://localhost:8080/Struts2Example

you will get a message and 404 error code is returned to the browser.

WARNING: Could not find action or result There is no Action mapped for namespace [/] and action name [] associated with context path [/Struts2Example]. - [unknown location]

The workaround is to add the file to the web root folder that will redirect a browser to the correct location.

index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=User/Login.action">
</head>

<body>
<p>Loading ...</p>
</body>
</html>

Also modify web application deployment descriptor to include this file name to the welcome files list.

web.xml:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

That's it, if you are looking for the Hello World tutorial, you should use these references:

  • Hello World
  • Hello World Using Struts 2
  • How To Create A Struts 2 Web Application
  • Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application