27 2月 2007

First Struts2 Project with Eclipse and Maven2

一個基本的Eclipse Struts Project至少需要以下的設定 pom.xml
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.elliot</groupId>
 <artifactId>firststruts</artifactId>
 <packaging>war</packaging>
 <name>firststruts</name>
 <version>1.0-SNAPSHOT</version>
 <url>http://maven.apache.org</url>
 <build>
  <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  <testSourceDirectory>
   ${basedir}/src/test/java
  </testSourceDirectory>
  <resources>
   <resource>
    <directory>${basedir}/src/main/resources</directory>
   </resource>
  </resources>
  <testResources>
   <testResource>
    <directory>${basedir}/src/test/resources</directory>
   </testResource>
  </testResources>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
     <warName>firststruts</warName>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-core</artifactId>
   <version>2.0.6</version>
  </dependency>
 </dependencies>
 <repositories>
  <repository>
   <id>Struts2</id>
   <name>Struts2</name>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
   <url>
    http://people.apache.org/repo/m2-snapshot-repository
   </url>
  </repository>
 </repositories>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
 xmlns="http://java.sun.com/xml/ns/j2ee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>firststruts</display-name>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <jsp-config>
  <taglib>
   <taglib-uri>/struts-tags</taglib-uri>
   <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
  </taglib>
 </jsp-config>
</web-app>
resources還需要有struts.properties struts.devMode = true struts.enable.DynamicMethodInvocation = false 然後實做自己的Action extends from com.opensymphony.xwork2.ActionSupport 加入到struts.xml中即可 struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="first" namespace="/" extends="struts-default">
        <action name="first" class="org.elliot.action.FirstAction">
            <result name="Success">/jsp/first/firstActionResult.jsp</result>
        </action>
    </package>
</struts>

沒有留言: