struts2约定插件无法正常工作 [英] struts2 conventions plugin not working properly

查看:302
本文介绍了struts2约定插件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用约定插件Struts2运行该应用程序.像这样配置struts.xml的应用程序很好:

I was trying to run the application with convention plugin Struts2. The application was fine with struts.xml configured like this:

<struts>

    <package name="struts2demo" extends="struts-default">
    <action name="hey" class="action.CountryAction" method="get">
       <result name="success">/index.jsp</result>
    </action>
    <action name="add" class="action.CountryAction" method="add">
       <result type="redirect" name="success">hey</result>
    </action>
    <!-- Add your actions here -->
    </package>

</struts>

现在我删除了该struts.xml并添加了一些注释,如下所示:

now I removed that struts.xml and added some annotations like this:

@Namespace("/")
@ResultPath(value="/")
public class CountryAction extends ActionSupport implements ModelDriven<Country>{
    private List<Country> worldCountry;
    private Country country = new Country();



    public Country getCountry() {
            return country;
        }

    public void setCountry(Country country) {
            this.country = country;
        }

 //   HttpServletRequest request;
@Action(value="/hey",results={@Result(name="success",location="/index.jsp")})
    public String get() throws SQLException
    {
        CountryService cs = new CountryService();
        setWorldCountry(cs.getCountry());
      //  System.out.println(getWorldCountry());
        return SUCCESS;
    }

     public List<Country> getWorldCountry() {
        return worldCountry;
    }

    public void setWorldCountry(List<Country> worldCountry) {
        this.worldCountry = worldCountry;
    }

    @Override
    public Country getModel() {
        return country;
    }
}

但是当我尝试运行该应用程序时,出现以下错误:

but when I am trying to run the application i am getting the following error:

消息:

There is no Action mapped for namespace [/] and action name [hey] associated with context path [/JustStruts2].

我的web.xml是这样:

My web.xml is this:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
         <param-name>struts.devMode</param-name>
         <param-value>true</param-value>
      </init-param>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

如果我做错了任何帮助,将不胜感激.
问候.

Where I am doing wrong, any help will be appreciated.
Regards.

推荐答案

根据Struts通知您[hey]的消息,在您的操作配置中找不到该消息.在struts.xml中,定义时没有斜杠.在注释中执行相同的操作.不要映射可能由容器本身而不是Struts2处理的index.jsp.默认情况下使用名称"success",所以没有必要.

According to the message the Struts inform you [hey] not found in your action configuration. In the struts.xml you defined it without slash. Do the same in the annotation. Don't map index.jsp that could be handled by the container itself but not by Struts2. The name "success" is used by default, so it's not necessary.

@Action(value="hey", results = { @Result(location="/page.jsp") })

请注意,不需要@ResultPath.

这篇关于struts2约定插件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆