maven搭建struts2

来源:互联网 发布:c语言顺序栈的实现 编辑:程序博客网 时间:2024/05/17 01:13

非常细致的步骤就不写了。只陈述一下基本的步骤以及要注意的事项。

1、新建Maven项目。(如果Eclipse已安装Maven插件,则“File->new->other->maven->maven Project“)。注意指定Archetype为maven-archetype-webapp。

2、打开POM.xml添加struts2的依赖。只需要三个:struts2-core,javassist,xwork-core。

3、配置web.xml。新建项目里,已自动生成web.xml在main/webapp/WEB-INF下。我们在<webapp></webapp>中间添加以下内容:

[xhtml] view plaincopyprint?
  1. <filter>  
  2.         <filter-name>struts2</filter-name>  
  3.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher  
  4.         </filter-class>  
  5.     </filter>  
  6.     <filter-mapping>  
  7.         <filter-name>struts2</filter-name>  
  8.         <url-pattern>/*</url-pattern>  
  9.     </filter-mapping>  

4、配置struts.xml。新建文件struts.xml,注意放在src/main/resources下。内容如下:

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.       
  6. <struts>  
  7.     <package name="default" extends="struts-default" namespace="/">  
  8.         <action name="login" class="loginAction" method="login">  
  9.             <result name="success">index.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts>  

5、编写loginAction类,注意不能放在src/main/resources下,因为这里的东西是资源,不会被当成代码来编译。要自己新建package。如src/main/java。添加后,设置项目的buildpath,添加src/main/java到Source。loginAction类:

[java] view plaincopyprint?
  1. public class loginAction {  
  2.     public String login(){  
  3.         return "success";  
  4.     }  
  5. }  

6、最后写一个index.jsp页面。注意放在webapp下。

 

项目启动后。访问localhost:8080/login就可以看到index.jsp页面的内容。

0 0
原创粉丝点击