随笔 - 16, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

Struts2 注解基础

The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]

先是报这个错,因为struts2的filter是*.action的原因。改为以下即可。

<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

 

1.convention plugin插件

convention plugin默认result页面存放在WEB-INF/content.(可以通过struts.convention.result.path属性来设置)。

如 http://localhost:8080/hello-world该url将访问WEB-INF/content/hello-world.jsp。

2.convention plugin查找类的规则

    convention plugin会查找struts、struts2、action、actions等包里的满足以下条件的类(好像可以设置)

  • 实现或继承com.opensymphony.xwork2.Action、ActionSupport的类。
  • 或者action结尾的类名
  修改包查找规则修改下面两个属性
<constant name="struts.convention.package.locators" value="test" />
<constant name="struts.convention.package.locators.basePackage" value="com.test" />

3.convention plugin类对应URL规则

  在struts、struts2、action、actions等包下生成“/”,更深层次则继续以“包名/”,类名子目全小写,按驼峰法分隔单词添加“-”。举例如下:

com.example.actions.MainAction -> /main
com.example.actions.products.Display -> /products/display
com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details

4.result对应页面名称

  页面命名与类名规则相同,再加上“-”与result值即可,如果找不到result的页面,似乎会返回到省略result名称的页面,还有success可省略,如下:

URLResultFile that could matchResult Type
/hello success /WEB-INF/content/hello.jsp Dispatcher
/hello success /WEB-INF/content/hello-success.htm Dispatcher
/hello success /WEB-INF/content/hello.ftl FreeMarker
/hello-world input /WEB-INF/content/hello-world-input.vm Velocity
/test1/test2/hello error /WEB-INF/content/test/test2/hello-error.html Dispatcher


5.chaining

  例子:foo action找不到result页面,会自动查找foo-bar action

package com.example.actions;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
@Action("foo")
public String foo() {
return "bar";
}

@Action("foo-bar")
public String bar() {
return SUCCESS;
}
}

 

 

  

  

 

 

 

 

参考:

struts2 convention-plugin文档

http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotation-example-1.html

http://apps.hi.baidu.com/share/detail/48320875

posted on 2011-12-30 20:14 yita 阅读(199) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: