I'm trying to implement the Struts 2 Annotations in my project, but I don't know how.
I added the convention-plugin v 2.1.8.1 开发者_如何学Pythonto my pom
I modified the web.xml
...
<init-param>
<param-name>actionPackages</param-name>
<param-value>org.apache.struts.helloworld.action</param-value>
</init-param>
...
My Action
package org.apache.struts.helloworld.action;
import org.apache.struts.helloworld.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
@Results({
@Result(name="success", location="HelloWorld.jsp")
})
public class HelloWorld extends ActionSupport {
public String execute() throws Exception {
messageStore = new MessageStore() ; return SUCCESS;
}
The jsp page from where I'm trying to use my action.
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='helloWorld'/>">Hello World</a></p>
</body>
When I press the link associated to the action helloWorld, but it's sends me to the exactly the same page. So, from index.jsp, it's sends to index.jsp.
The way it should behave: it should send me to HelloWorld.jsp.
I uploaded the project (a very simple HelloWorld app) to FileFront, maybe someone sees where is the problem. http://www.filefront.com/16364385/Hello_World.zip
Convention uses a different convention to convert CamelCaseAction names to url's and jsp's names. If you are using Convention's defaults, I believe you should have used the following names:
ActionClass: HelloWorldAction.java
JSP: hello-world.jsp
Action: hello-world
Also, notice that by default convention will look for your JSPs on WEB-INF/content . The documentation is a bit shallow, you have to understand by the examples, but you can consult all default values there: http://struts.apache.org/2.x/docs/convention-plugin.html
I havent used Struts2 with annotations (which Struts2 version? are you following some tutorial or doc?) . But should not that location attribute (in the Result annotation) be instead value ?
What do the logs say? Have you tried using /HelloWorld.jsp for "success". I think that struts framework does not find the resource and is loading the same page.
When you use /HelloWorld.jsp, hopefully you will see the result page.
精彩评论