创建完web工程之后,一般我们就可以开始开发了。所以,这一节我们需要为这个web工程加入j2ee框架的支持。
这里其实不太好分清楚先加哪后加哪,因为系统框架是需要整体运行的,相互的关系很紧密,比如我只加入struts2.那么在运行时会报错。因为struts2缺省是用spring来做容器的。(当然也可以修改,但这里不是不作讨论)。
一、依赖包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-codebehind-plugin</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.5.ga</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
以上不止加入struts支持,也加入了spring和hibernate及jpa的支持。其实这些只是主包,还有不少依赖包会自动加入到maven的仓库里去的。
二、配置struts
下面配置struts2,涉及到几个文件
1、在web.xml加入以下内容:
<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>
2、增加struts.xml文件
这个文件主要配置struts的action及拦截器等,可以有多个(命名为struts-*.xml)
位置在src\main\resources下,内容大致如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Constants -->
<constant name="struts.devMode" value="false"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.action.extension" value="html"/>
<constant name="struts.objectFactory" value="spring"/>
<constant name="struts.custom.i18n.resources" value="ApplicationResources,errors"/>
<constant name="struts.multipart.maxSize" value="2097152"/>
<constant name="struts.ui.theme" value="css_xhtml"/>
<constant name="struts.codebehind.pathPrefix" value="/WEB-INF/pages/"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<interceptors>
<!-- Copied from struts-default.xml and changed validation exclude methods -->
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servlet-config"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scoped-model-driven"/>
<interceptor-ref name="model-driven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="static-params"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">cancel,execute,delete,edit,list</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<global-results>
<result name="mainMenu" type="redirect-action">mainMenu</result>
<result name="dataAccessFailure">/WEB-INF/pages/dataAccessFailure.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="org.springframework.dao.DataAccessException" result="dataAccessFailure"/>
</global-exception-mappings>
<action name="addUser" class="userAction" method="save">
<result name="success">/WEB-INF/pages/user/list.jsp</result>
<result name="input">/WEB-INF/pages/user/form.jsp</result>
</action>
<action name="toAddUser">
<result>/WEB-INF/pages/user/form.jsp</result>
</action>
<!-- Add additional actions here -->
</package>
</struts>
3、添加国际化支持
也就是加几个资源文件主要是:
ApplicationResources.properties
ApplicationResources_zh_CN.properties
这个就不我说了,文件位置在src\main\resources下
4、添加action
以用户为例UserAction.java:
package net.apex.framework.web.action;
import net.apex.framework.model.User;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class UserAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String save(){
System.out.println(user.getName());
return SUCCESS;
}
}
struts配置完毕
三、配置spring
1、web.xml
加入以下代码:
<!--配置spring上下文(可选)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!--加入spring支持-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、添加ApplicationContext.xml
这个也可以有多个,内容大致如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="show_sql">true</prop>
</props>
<!-- Turn batching off for better error messages under PostgreSQL -->
<!-- hibernate.jdbc.batch_size=0 -->
</property>
</bean>
<bean id="userAction" scope="prototype" class="net.apex.framework.web.action.UserAction"></bean>
</beans>
这样struts、spring和hibernate框架都加入到我们的工程里了。
在工程里添加一个jsp页面让它调用UserAction就可以跑起来了。。。