以下内容参考网上的资料
间接实现零配置
struts.xml
<action name="*/*" method="{2}" class="workbench.web.actions.{1}Action"> <result name="custom">/view/{1}/${target}.jsp</result> </action>
编写一个基础的Action
public abstract class BaseAction { protected final String CUSTOM = "custom"; private String target; protected final Log logger = LogFactory.getLog(getClass()); public String getTarget() { return target;} public void setTarget(String target) { this.target = target;} protected String render(String _target){ setTarget(_target); return CUSTOM;} }
public class UserAction extends BaseAction{ private User user; private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public User getUser() { return user;} public void setUser(User user) { this.user = user; } public String test(){ user = userService.get(1l); return render("test"); } }
访问:http://localhost:8080/workbench/User/test
真正的夫配置,它是基于包的路径的
原文:http://www.iteye.com/post/390374
web.xml:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>demo.actions1,demo.actions2</param-value> </init-param> </filter>
Struts2扫描到Action后,从actionPackages指定的包开始,子包名会成为这个Action的namespace,而Action的name则由这个Action的类名决定。将类名首字母小写,如果类名以Action结尾,则去掉"Action"后缀,形成的名字就是这个Action的名字。在如上所述的示例中,actionPackages指定为demo.actions1,demo.actions2,那么你可以这样访问demo.actions1.app.person.ListPeopleAction:
http://localhost:8080/app/person/listPeople
Struts2是通过"Result"和"Results"两个类级别的annotations来指定Results的。
作为示例,我们在webapp目录下建两个html文件:success.html和error.html,随便写点什么内容都可以。现在假设我们访问/app/person/listPeople时,或Action返回success就转到success.html页面,若是error就转到error.html页面,这只需要在ListPeopleAction类上加上一段注解就可以了: