SSH框架搭建例子
框架整合及开发步骤
一、开发工具2
二、开发框架2
2.1、Struts2.0.142
2.2、Spring2.5.42
2.3、Hibernate3.2.53
2.4、jQuery4
三、所需jar包4
四、项目目录5
五、整合步骤6
5.1、spring6
5.2、struts7
5.3、hibernate7
六、开发步骤11
6.1、配置xml文件11
6.2、编写pojo对象并生成与数据库对应的hbm.xml文件。16
6.3、编写操作数据库的Dao类16
6.4、编写业务逻辑代码18
6.5、编写action类20
6.6、编写JSP页面23
一、开发工具
MyEclipse7.0
MySql5.0
JDK1.6
Jboss5.0
二、开发框架
2.1、Struts2.0.14
Struts是一个在JSP Model2基础上实现的MVC框架,主要分为模型(Model)、视图(Viewer)和控制器(Controller)三部分,其主要的设计理念是通过控制器将表现逻辑和业务逻辑解耦,以提高系统的可维护性、可扩展性和可重用性。Struts框架的体系结构如图所示:
视图(Viwer):视图部分主要由JSP页面组成,其中不包括业务逻辑和持久化操作,数据由struts标签或jstl标签呈现在页面上。
控制层(Controller):控制层由FilterDispatcher组成,FilterDispatcher负责过滤接收HTTP请求,然后根据配置文件(struts.xml)调用相关的Action。
模型(Model):struts1中有ActionFormBean作为模型,但在struts2中,ActionFormBean与Action合并,就没有严格意义上的模型了。一般由持久化对象作为模型。
2.2、Spring2.5.4
Spring是一个轻量级的MVC框架,在SSH框架整合中,Spring作为一个管理者,管理和提供项目中的所有组件。Spring框架体系结构如图:
2.3、Hibernate3.2.5
Hibernate对数据进行持久化,运用O/R mapping对关系型数据库和java对象进行映射,简化了开发人员对数据库的操作。Hibernate原理图如下:
2.4、jQuery
Jquery是继prototype之后又一个优秀的Javascrīpt框架。它是轻量级的js库,兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。jQuery使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。jQuery能够使用户的html页保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需定义id即可。
只需要在页面上导入jQuery的包就可以使用该框架了,具体的语法参照http://www.w3school.com.cn/jquery/
三、所需jar包
dom4j.jar
hibernate.jar
log4j.jar
mysql-connectior-java.jar
proxool.jar
proxool-cglib.jar
spring-beans.jar
spring-context.jar
spring-core.jar
spring-orm.jar
spring-tx.jar
spring-web.jar
spring-webmvc-struts.jar
struts2-core.jar
struts2-spring-plugin.jar
xwork.jar
commons-logging.jar
jta.jar
commons-collections.jar
cglib-nodep.jar
antlr.jar
四、项目目录
java目录
说明:
config:存放项目的配置文件
common:存放公共组件,如影片关联的显示,打印,日志导出等等
action:存放action模块
service:存放业务逻辑模块
hibernateDao:存放对数据库的操作类
entity:存放POJO对象及hbm.xml文件
exception:存放自定义的异常类
log:存放项目日志
页面目录
hotelPortal:存放Portal端的页面
hotelmanager:存放管理端的页面
js:存放js文件
css:存放css样式文件
五、整合步骤
5.1、spring
在web.xml中加入下面的监听代码告知项目使用spring框架:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
同时设置application-context.xml文件的位置以便系统加载,如果不做该设置,系统默认加载/WEB-INF/applicationContext.xml文件:
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
classpath*:/application/application-context.xml
</param-value>
</context-param>
application-context.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
</beans>
现在spring框架就搭建好了。
5.2、struts
struts1与struts2相比,变化比较大,在web.xml文件中, struts1采用<servlet>的方式,而struts2中要配置成<filter>形式,具体配置如下:
<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>
struts1中配置文件为struts-config.xml,路径为/WEB-INF/struts-config.xml,在struts2中配置文件为struts.xml,路径为/WEB-INF/classes/struts.xml,空的struts.xml内容为:
<?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>
</struts>
5.3、hibernate
在这里配合proxool数据库连接池一起使用,首先在web.xml中加入proxool的配置:
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>/WEB-INF/proxool.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
创建proxool.xml文件用来配置数据库连接信息,并将该文件放在/WEB-INF目录下
proxool.xml:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>dbname</alias>
<driver-url>jdbc:mysql://192.168.111.204:3306/lws</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</driver-properties>
<maximum-connection-count>1000</maximum-connection-count>
<minimum-connection-count>100</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>10</maximum-new-connections>
<prototype-count>10</prototype-count>
<simultaneous-build-throttle>20</simultaneous-build-throttle>
<test-before-use>false</test-before-use>
<house-keeping-test-sql>select 1 from dual</house-keeping-test-sql>
</proxool>
</something-else-entirely>
如果将proxool.xml放置在其他位置,需要在<param-value>/WEB-INF/proxool.xml</param-value>处指定位置。
创建hibernate-connection.xml文件,放在与application-context.xml同级的目录下
hibernate-connection.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="dataSource" value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.dbname" />
</bean>
<bean name="sessionFactory" scope="singleton" lazy-init="true" ref="dataSource" />
<property name="mappingResources" ref="hibernateMappingResources" />
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
<bean name="hibernateMappingResources" scope="singleton" lazy-init="true"
value="java.util.ArrayList" />
<property name="sourceList">
<list>
<!—- 这里是对象的hbm.xml文件 -->
<value>XXX/XXX.hbm.xml</value>
</list>
</property>
</bean>
<bean name="hibernateProperties" scope="singleton" lazy-init="true"
encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="entity.student.StudentImpl" table="student">
<id name="id" type="java.lang.Long" column="id">
<generator type="string" column="name" not-null="true" length="200"/>
<property name="name_1" type="string" column="name_1" not-null="false" length="200"/>
<property name="flag" type="string" column="flag" not-null="false" length="200"/>
</class>
</hibernate-mapping>
将对象hbm.xml文件的相对路径配置在hibernate-connection.xml里。
在spring的配置文件application-context.xml中加入hibernate-connection.xml的配置,使得加载spring同时加载hibernate。
六、开发步骤
6.1、配置xml文件
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/kt/hotel/config/application.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/classes/com/kt/hotel/config/proxool.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
用struts.xml来管理各个模块的struts配置
<?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>
<include file="com/kt/hotel/config/struts-movie.xml"></include>
</struts>
struts-movie.xml:
<?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>
<package name="hotel" namespace=""
extends="struts-default">
<action name="movie" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="requiredChecker" />
<import resource="classpath:com/kt/hotel/common/common.xml"/>
<import resource="classpath:com/kt/hotel/config/action.xml"/>
<import resource="classpath:com/kt/hotel/config/hibernate-connection.xml"/>
<import resource="classpath:com/kt/hotel/config/hibernateDao.xml"/>
<import resource="classpath:com/kt/hotel/config/service.xml"/>
</beans>
hibernate-connection.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="dataSource"
value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.dbname" />
</bean>
<bean id="sessionFactory" scope="singleton" lazy-init="true"
ref="dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>/WEB-INF/classes/com/kt/hotel/entity/hbm</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
<bean id="hibernateProperties" scope="singleton" lazy-init="true"
encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>dbname</alias>
<driver-url>jdbc:mysql://192.168.211.204:3306/hoteldatabase</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</driver-properties>
<maximum-connection-count>500</maximum-connection-count>
<minimum-connection-count>100</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>200</maximum-new-connections>
<prototype-count>100</prototype-count>
<simultaneous-build-throttle>200</simultaneous-build-throttle>
<test-before-use>false</test-before-use>
<house-keeping-test-sql>select 1 from dual</house-keeping-test-sql> </proxool>
</something-else-entirely>
6.2、编写pojo对象并生成与数据库对应的hbm.xml文件。
POJO对象和相应的hbm.xml文件已经建好,这里不做过多阐述。
6.3、编写操作数据库的Dao类
具体的数据库连接、释放等操作交给hibernate和数据库连接池去完成,下面以对影片对象进行添加和查询为例:
首先创建EntityDao类,该类为抽象类,继承了HibernateDaoSupport类,且只有一个SessionFactory参数
EntityDao.java:
public abstract class EntityDao extends HibernateDaoSupport
{
@SuppressWarnings("unused")
private SessionFactory sessionFactory;
}
然后在config文件下创建hibernateDao.xml文件来管理dao的配置,在该xml文件中配置EntityDao并且注入sessionFactory
HibernateDao.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<import resource="classpath*:com/kt/hotel/hibernateDao/hibernateDao-movie.xml"/>
<bean id="entityDao" scope="singleton" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
注意:这里EntityDao要用abstract=”true”配置成抽象的。
接下来编写MovieDaoImpl类,该类是movie的dao类,继承抽象类EntityDao
MovieDaoImpl.java:
public class MovieDaoImpl extends EntityDao implements MovieDao{
public void add(MoviePO movie)
{
this.getHibernateTemplate().save(movie);
}
@SuppressWarnings("unchecked")
public List<MoviePO> getList()
{
String hql = "from MoviePO";
List<MoviePO> list = (List<MoviePO>)this.getHibernateTemplate().find(hql);
return list;
}
}
实现类MovieDaoImpl放在文件夹hibernateDao下的Impl目录里,接口类放在hibernateDao目录下,下面创建hibernateDao-movie.xml文件来管理影视的dao类
hibernateDao-movie.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="movieDao" scope="singleton" parent="entityDao">
</bean>
</beans>
其中parent="entityDao"表示继承前面所配置的EntityDao类
6.4、编写业务逻辑代码
在service目录下创建impl目录,该目录存放业务逻辑的实现类。创建MovieServiceImpl.java类来调用MovieDao中的添加和显示方法,
MovieServiceImpl.java:
public class MovieServiceImpl implements MovieService {
private MovieDao movieDao;
public void add(MoviePO movie)
{
movieDao.add(movie);
}
public List<MoviePO> getList()
{
List<MoviePO> list = movieDao.getList();
return list;
}
@Required
public void setMovieDao(MovieDao movieDao) {
this.movieDao = movieDao;
}
}
在service目录下创建service-movie.xml管理影视的业务逻辑
service-movie.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="movieService" scope="singleton">
<property name="movieDao" ref="movieDao" />
</bean>
</beans>
接着在config目录下创建service.xml文件管理各个模块的service-xxx.xml文件
service.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<import resource="classpath*:com/kt/hotel/service/service-movie.xml"/>
</beans>
6.5、编写action类
struts2.0与struts1.x差别很大,在struts2.0中action不需要继承任何类,切方法返回值是字符串,通过返回的字符串来找到相应的跳转页面,
MovieAction.java:
public class MovieAction {
private MovieService movieService;
private MoviePO movie;
public String add()
{
MoviePO entity = (MoviePO)movie;
movieService.add(entity);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("forwardPage", "movie!getList.action");
return "addSuccess";
}
public String getList()
{
List<MoviePO> list = movieService.getList();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("movieList", list);
return "movieList";
}
public String forwardAddPage()
{
return "addMoviePage";
}
@Required
public void setMovieService(MovieService movieService) {
this.movieService = movieService;
}
public void setMovie(MoviePO movie) {
this.movie = movie;
}
public MoviePO getMovie() {
return movie;
}
}
其中MovieService是要注入的影视业务类,MoviePO是POJO对象,如果用request,需通过ServletActionContext来获取。
在action目录创建action-movie.xml文件来管理影视的action
action-movie.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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<bean id="movieAction" scope="prototype">
<property name="movieService" ref="movieService" />
</bean>
</beans>
注意:这里的id=”movieAction”的值要与struts.xml中配置的class相一致,
如movieAction对应struts.xml中
<action name="movie" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<import resource="classpath:com/kt/hotel/action/action-movie.xml"/>
</beans>
访问路径是由工程名,struts.xml配置的action对应的name和Action类中的方法名来指定的,如上例,工程名为hotel,struts.xml中action定义的name是movie,对应action类MovieAction.java中的方法名是getList()和add()如果要调用getList()方法,url为:http://<ip>:<port>/hotel/movie!getList.action,如果要调用add方法,路径为
http://<ip>:<port>/hotel/movie!add.action
6.6、编写JSP页面
在JSP页面使用jstl标签来显示内容。首先在JSP页面加入jstl标签库
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="s" uri="/struts-tags" %>
然后就可以使用这些标签了,下面是在movieList.jsp页面用<c:forEach>来循环显示
<c:forEach items="${movieList}" var="movie">
<tr align="center">
<td width="5%"><input type="checkbox" name="movie"></td>
<td width="10%">${movie.name}</td>
<td width="15%">${movie.actor}</td>
<td width="15%">${movie.price}</td>
<td width="10%"><a href="javascript:void(0);">修改</a></td>
</tr>
</c:forEach>
其中movieList是在action类中,通过request.setAttribute传到页面上的列表。
addMovie.jsp页面中<input type="text" name="movie.name" size="20">的movie.name要注意,这是struts2.0的新写法,movie对应于MovieAction中定义的movie属性,和setMovie和getMovie方法一起构成了formBean,页面上通过movie.name标示,把值映射到MoviePO对象的name属性中。