struts-2.1.8.1
hibernate-distribution-3.6.8.Final
spring-framework-3.0.6.RELEASE
需要的jar包请到官网下载
首先在Eclipse里新建一个web项目,起名为sshdemo吧。建好后结构如下:
解压下载的struts2包,将lib目录下的以下jar包复制到项目的lib目录下,这些是struts2必须的jar包:
在web.xml中添加以下代码:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
src目录下新建struts.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
</struts>
好了,必须工作完成了,来做点东西,添加个页面,输入名字,然后跳转到个问候页(好老套)
在WebContent新建一个index.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>欢迎</title>
</head>
<body>
<s:form name="form1" action="/sayHello.action">
<s:textfield name="name" label="your name"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
在添加一个welcome.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>欢迎</title>
</head>
<body>
Hello, <s:property value="name"/>
</body>
</html>
新建test.action包,在下面建一个类SayHello,继承ActionSupport类,重写execute方法,内容如下:
package test.action;
import com.opensymphony.xwork2.ActionSupport;
public class SayHello extends ActionSupport{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
struts.xml里加上这样一段:
<package name="test" extends="struts-default">
<action name="sayHello" class="test.action.SayHello">
<result name="success">/welcome.jsp</result>
</action>
</package>
将项目部署到tomcat,启动,没问题。在浏览器输入:http://localhost:8083/sshdemo/(8083是我的tomcat端口,默认的是8080),系统应该自动跳转到index.jsp,但是报错了:
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
原因是不经过struts过滤而直接访问页面的话如果页面上使用了struts标签就会出错。解决方法:
1、不使用struts标签;
2、让struts过滤所有的请求,将web.xml里的<url-pattern>*.action</url-pattern>改为<url-pattern>/*</url-pattern>;
3、在struts.xml的配置里加上这么一段:
<action name="index">
<result>/index.jsp</result>
</action>
这样我们访问http://localhost:8083/sshdemo/index.action就可以了。访问后页面如下:
输入名字,点击submit按钮:
OK了,struts2已将添加进web项目。
接下来是spring,将下载的spring文件解压,到dist目录,将所有jar包复制到项目的lib目录下。
另外还要把struts2-spring-plugin-2.1.8.1.jar也复制过来。
在web.xml里添加以下代码:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-*.xml,**/spring-*.xml
</param-value>
</context-param>
其中,classpath:spring-*.xml,**/spring-*.xml的意思是加载项目下所有的spring-开头的xml文件作为spring的配置文件。
关于spring的配置文件,有的人喜欢写在一个文件里,有的人喜欢写在不同的文件里。写在一个文件里可以避免文件过于分散,出现一些像bean定义重复的的问题,但是配置文件显得很臃肿。根据个人爱好吧。我选择的是写在不同的文件里。
在test包下建一个spring-test.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="sayHelloAction" class="test.action.SayHello" scope="prototype"/>
</beans>
修改struts.xml,将action的class改为sayHelloAction,也就是在spring配置文件里定义的beanid:
<action name="sayHello" class="sayHelloAction">
<result name="success">/welcome.jsp</result>
</action>
再启动tomcat试一下,功能跟刚才一样,spring也添加进来了。
最后添加Hibernate。把Hibernate包复制到lib目录,必须的有以下jar包:
另外,我们还需要额外的两个jar包:commons-dbcp.jar和commons-pool-1.3.jar,我用的是oracle,还要添加oracle的驱动ojdbc14.jar。然后在数据库建好用户,我们的用户就叫sshdemo,密码1。新建一张表PERSON,字段有ID,NAME,AGE。
在WEB-INFO下建一个spring-datasource.xml,我们在里面配置数据库相关内容。oracle的配置方法,内容如下:
<?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" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:XE</value>
</property>
<property name="username">
<value>sshdemo</value>
</property>
<property name="password">
<value>1</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:**/po/</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialet">org.hibernate.dialect.OracleDialet</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
其中:
<property name="mappingDirectoryLocations">
<list>
<value>classpath:**/po/</value>
</list>
</property>
作用是搜索所有po文件夹下的hbm.xml映射文件。
Hibernate必须的内容准备好了。下面我们在做一点事情,首页传过用户名后我们将它保存在数据库中。
在test包下新建po包,用来存放bean和映射文件:
Person.java,我又增加了个age字段,内容如下:
package test.po;
import java.math.BigDecimal;
public class Person implements java.io.Serializable {
private String id;
private String name;
private BigDecimal age;
public Person() {
}
public Person(String name, BigDecimal age) {
this.name = name;
this.age = age;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getAge() {
return this.age;
}
public void setAge(BigDecimal age) {
this.age = age;
}
}
然后是映射文件Person.hbm.xml,内容如下:
<?xml version="1.0" 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="test.po.Person" table="PERSON">
<id name="id" type="string">
<column name="ID" length="72" />
<generator class="uuid.hex"></generator>
</id>
<property name="name" type="string">
<column name="NAME" length="50" not-null="true" />
</property>
<property name="age" type="big_decimal">
<column name="AGE" precision="22" scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>
index.jsp改一下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
</head>
<body>
<s:form name="form1" action="/sayHello.action">
<s:textfield name="person.name" label="username"></s:textfield>
<s:textfield name="person.age" label="age"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
在test包下新建dao包,新建接口SayHelloDAO.java,内容如下:
package test.dao;
import test.po.Person;
public interface SayHelloDAO {
public void add(Person person) throws Exception;
}
在dao包下再新建SayHelloDAO的实现类SayHelloDAOImpl.java,继承HibernateDaoSupport,内容如下:
package test.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import test.dao.SayHelloDAO;
import test.po.Person;
public class SayHelloDAOImpl extends HibernateDaoSupport implements SayHelloDAO{
@Override
public void add(Person person) throws Exception {
this.getHibernateTemplate().save(person);
}
}
为了简便,我们直接在action中调用dao来完成保存操作吧。修改SayHello.java,内容如下:
package test.action;
import test.dao.SayHelloDAO;
import test.po.Person;
import com.opensymphony.xwork2.ActionSupport;
public class SayHello extends ActionSupport{
private Person person;
private SayHelloDAO sayHelloDAO;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public SayHelloDAO getSayHelloDAO() {
return sayHelloDAO;
}
public void setSayHelloDAO(SayHelloDAO sayHelloDAO) {
this.sayHelloDAO = sayHelloDAO;
}
@Override
public String execute() throws Exception {
sayHelloDAO.add(person);
return SUCCESS;
}
}
修改spring-test.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="sayHelloAction" class="test.action.SayHello" scope="prototype">
<property name="sayHelloDAO" ref="sayHelloDAO"></property>
</bean>
<bean id="sayHelloDAO" class="test.dao.impl.SayHelloDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
好了,启动项目,输入内容,提交,后台打印Hibernate: insert into PERSON (NAME, AGE, ID) values (?, ?, ?),数据库里也插入了数据。OK了。
太晚了,先到这,未完待续……