`

struts2真正零配置,没有struts.xml

阅读更多

    信徒疑惑问:struts2零配置,能实现真正没有配置文件吗?

    佛说:嗯!

    

     什么个情况?真的?传闻不都是说,不能实现吗?只是一次性配置吗?

     

     如果你这么认为,你就不懂得用struts2的零配置。附件中的项目连struts.xml都没有,照样能用?why?struts2的约定大于配置规则。如果你没有配置,就按约定。

    佛说“嗯”的意思是什么?第一:可以不要配置;第二:实际项目中,我们会有一些特定需求,其次,为了项目的需要,想改约定规则,我们还是会加配置,但并不是不能实现零配置。

 

 

注意:开发环境是myeclipse10 + jdk1.6

 

 

访问路径:http://localhost:8080/struts2-demo/demo!test.action           demo-test.jsp

访问路径:http://localhost:8080/struts2-demo/demo!hello.action          demo-hello.jsp

访问路径:http://localhost:8080/struts2-demo/demo!sayno.action        demo.jsp   //看代码注释

 

 

 

 

 

DemoAction

 package lp.action;

 

import com.opensymphony.xwork2.ActionSupport;

 

 

/**

 * 

 * 用于测试的action

 * 零配置测试

 * @author liaopin

 *

 */

public class DemoAction extends ActionSupport{

private static final long serialVersionUID = 1L;

 

public String test(){

System.out.println("Login in action");

return "test";

}

 

public String hello(){

System.out.println("Login in action hello");

return "hello";

}

 

public String sayno(){

System.out.println("Login in action sayno");

return "nulljsp";//如果没有demo-nulljsp.jsp,默认访问demo.jsp,如果两者都没有,会报找不到对应视图

}

}

 

 

pom.xml

 

 <!-- struts2 -->

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-core</artifactId>

<version>2.3.4.1</version>

</dependency>

<!-- Convention 插件默认扫描继承了action类的子类和文件名以Action结尾的文件

 

默认找的包是struts, struts2, action or action的包 -->

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-convention-plugin</artifactId>

<version>2.3.4.1</version>

</dependency>

 

 

 

struts2零配置约定:关于约定的东西,网上一大把,能自己动手就动手找下度娘吧,跟她搞熟!

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics