1、新建Java Web工程
2、添加Struts2.1框架支持
去除冲突包:antlr-2.7.2.jar
一般项目加上这三个包足够,后期可以视项目需求增加支持包:
2.1配置web.xml文件
增加struts2的配置,增加容器对struts2的初始化。增加代码如下:这些配置代码对于struts2是不变的,直接复制到web.xml即可。
- <!-- struts2过滤器,对任何请求都添加这个过滤器 -->
- <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>/*</url-pattern>
- </filter-mapping>
2.2配置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>
3、添加Spring框架支持
一般添加这6个支持包
效果图:
3.1配置web.xml文件
初始化加载spring资源配置文件
- <!-- 指定spring的配置文件,默认从web根目录寻找配置文件-->
- <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>
- <listener>
- <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
- </listener>
4、配置数据源
此处用oracle数据库作为数据源:
- create user ssh identified by 1234;
- grant dba to ssh with admin option;
- create table people (
- id varchar2(32) primary key,
- username varchar2(32),
- password varchar2(32)
- );
- insert into PEOPLE (ID, USERNAME, PASSWORD) values ('1', 'John', '1234');
- insert into PEOPLE (ID, USERNAME, PASSWORD) values ('2', 'Tom', '1234');
驱动包直接用oracle自带的jdbc驱动包即可,路径如上图所示。
5、添加Hibernate框架支持
删除冲突包:asm.jar和cglib-2.1.3.jar
切换到MyEclipse Database Explorer视图:
选择我们建立的链接SSH,点开,找到对应的table,people,右键
6、代码设计
为了方便管理代码,先建立四个package,如下图:
在com.ssh.dao下建立一个接口,IPeopleDAO.java
- package com.ssh.dao;
- import java.util.List;
- import com.ssh.people.People;
- public interface IPeopleDAO {
- public People findById(String id);
- public List findByUsername(Object username);
- public void save(People people);
- }
把PeopleDAO拖到com.ssh.dao.impl下
PeopleDAO.java里要加implements IPeopleDAO
修改后:
在com.ssh.service下建立IPeopleService.java
- package com.ssh.service;
- import com.ssh.people.People;
- public interface IPeopleService {
- public People getPeopleById(String id);
- public People getPeopleByUsername(String username);
- public void addPeople(People people);
- }
在com.ssh.service.impl下建立PeopleService.java
- package com.ssh.service.impl;
- import java.util.List;
- import com.ssh.dao.IPeopleDAO;
- import com.ssh.people.People;
- import com.ssh.service.IPeopleService;
- public class PeopleService implements IPeopleService{
- private IPeopleDAO peopleDAO;
- public People getPeopleById(String id) {
- return peopleDAO.findById(id);
- }
- public People getPeopleByUsername(String username) {
- List list = peopleDAO.findByUsername(username);
- if(list.size() == 0){
- return null;
- }else{
- return (People)list.get(0);
- }
- }
- public void addPeople(People people) {
- peopleDAO.save(people);
- }
- public IPeopleDAO getPeopleDAO() {
- return peopleDAO;
- }
- public void setPeopleDAO(IPeopleDAO peopleDAO) {
- this.peopleDAO = peopleDAO;
- }
- }
效果图:
接着要在applicationContext.xml中添加一个bean的信息
- <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
- <property name="peopleDAO">
- <ref bean="PeopleDAO"></ref>
- </property>
- </bean>
整个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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="oracle.jdbc.OracleDriver">
- </property>
- <property name="url"
- value="jdbc:oracle:thin:@localhost:1521/orcl">
- </property>
- <property name="username" value="ssh"></property>
- <property name="password" value="1234"></property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.Oracle9Dialect
- </prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/ssh/people/People.hbm.xml</value></list>
- </property>
- </bean>
- <bean id="PeopleDAO" class="com.ssh.dao.impl.PeopleDAO">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
- <property name="peopleDAO">
- <ref bean="PeopleDAO"></ref>
- </property>
- </bean>
- </beans>
创建处理action的文件,新建com.ssh.action包,新建LoginAction.java文件
- package com.ssh.action;
- import com.opensymphony.xwork2.ActionSupport;
- import com.ssh.people.People;
- import com.ssh.service.IPeopleService;
- public class LoginAction extends ActionSupport{
- private String username;
- private String password;
- private IPeopleService peopleService;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public IPeopleService getPeopleService() {
- return peopleService;
- }
- public void setPeopleService(IPeopleService peopleService) {
- this.peopleService = peopleService;
- }
- public String execute(){
- People people = peopleService.getPeopleByUsername(username);
- if(people == null) return "error";
- if(people.getPassword().equals(password))
- return "success";
- else return "error";
- }
- public void validate(){
- if(username == null || username.length() == 0){
- super.addActionError("用户名不能为空");
- }
- if(password == null || password.length() == 0){
- super.addActionError("密码不能为空");
- }
- }
- }
配置applicationContext.xml文件:
- <bean id="loginAction" class="com.ssh.action.LoginAction" scope="prototype">
- <property name="peopleService" ref="peopleService"></property>
- </bean>
配置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>
- <package name="com" extends="struts-default" namespace="/">
- <action name="login" class="loginAction">
- <result name="success">/index.jsp</result>
- <result name="input">/login.jsp</result>
- <result name="error">/login.jsp</result>
- </action>
- </package>
- </struts>
新增login.jsp文件:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib prefix="s" uri="/struts-tags"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>登录页面</title>
- </head>
- <body>
- <s:form name="form" action="login.action" method="post">
- <s:textfield name="username" label="username"></s:textfield>
- <s:password name="password" label="password"></s:password>
- <s:submit label="submit"></s:submit>
- </s:form>
- <s:actionerror/>
- </body>
- </html>
6.1完整的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>
- <package name="com" extends="struts-default" namespace="/">
- <action name="login" class="loginAction">
- <result name="success">/index.jsp</result>
- <result name="input">/login.jsp</result>
- <result name="error">/login.jsp</result>
- </action>
- </package>
- </struts>
6.2完整的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">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <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>/*</url-pattern>
- </filter-mapping>
- <!-- 指定spring的配置文件,默认从web根目录寻找配置文件-->
- <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>
- <listener>
- <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
- </listener>
- </web-app>
6.3完整的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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="oracle.jdbc.OracleDriver">
- </property>
- <property name="url"
- value="jdbc:oracle:thin:@localhost:1521/orcl">
- </property>
- <property name="username" value="ssh"></property>
- <property name="password" value="1234"></property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.Oracle9Dialect
- </prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/ssh/people/People.hbm.xml</value></list>
- </property>
- </bean>
- <bean id="PeopleDAO" class="com.ssh.dao.impl.PeopleDAO">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <bean id="peopleService" class="com.ssh.service.impl.PeopleService">
- <property name="peopleDAO">
- <ref bean="PeopleDAO"></ref>
- </property>
- </bean>
- <bean id="loginAction" class="com.ssh.action.LoginAction" scope="prototype">
- <property name="peopleService" ref="peopleService"></property>
- </bean>
- </beans>
7、部署运行
7.1Tomcat服务器
7.2去除antlr-2.7.2.jar冲突包
apache-tomcat-7.0.54webappsSSH_DemoWEB-INFlib目录下
7.3整理jar包
把apache-tomcat-7.0.54webappsSSH_DemoWEB-INFlib目录下的jar包全部复制出来,这个就是SSH框架所需的jar包。
然后把jar.zip中的jar包导入即可。
7.4运行访问
8、常见问题
1) 关于 myeclipse 里面没有 add struts capabilities 问题解决方法
高版本的 myeclipse 里面的 add struts capabilities 在如下路径中:(当你加载过一次之后,这个 add struts capabilities就不会出现。
解决方法:把 .project 文件的关于struts的配置删除掉即可