所谓间接实现零配置,是指只要做些初始化的配置之后,在以后的开发中基本上不用再对每个Action做配置
struts.xml这样配置
代码
- <action name="*/*" method="{2}" class="workbench.web.actions.{1}Action">
- <result name="custom">/view/{1}/${target}.jsp</result>
- </action>
struts.properties的配置:
代码
- struts.objectFactory = spring
- struts.objectFactory.spring.autoWire = name
- struts.devMode = true
- struts.enable.DynamicMethodInvocation = false
- struts.action.extension =
- struts.enable.SlashesInActionNames = true
然后写一个BaseAction:
代码
- 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;
- }
- }
这样其余的Action都可以直接继承BaseAction,不用再做任何配置
通过return render(target)转发到指定的jsp页面,从而间接实现零配置
代码
- 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");
- }
- }