(一)关键理念及需要注意的地方:

  使用struts2+spring3.0的框架搭建web程序,就是使用spring来进行依赖注入(依赖注入请参考baidu上面的解释:http://baike.baidu.com/link?url=uESWlODOsyqaaqlGLxps8xh2UaadfEe2rdsjspvZN5qsw1BOitPx_QQYuPV904jCwb493WK1ROrO3iIPZrbAQa)。

  需要注意的地方如下:

  1. struts.xml中的action的class要写spring中的bean,这个意思就是让spring去实例化改对象。比如:<action name="Login" class="loginAction" method="execute">
  2. 在action的bean中在引入相关的类
        <bean id="loginAction" class="com.HYOpticalComm.action.LoginAction"> 
      <property name="loginService" ref="loginServiceImp"></property>  ---这里边调用的是LoginAction类的setter函数。并使用loginServiceImp类来实例化
        </bean>
  3. 要包含“Struts 2 Spring”这个包,这个包就是struts和spring关联的包。

(二)目录结构和需要的包:

  

(三)各个文件相互之间的关系:

(四)代码:

<?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"> <!-- 配置FilterDispatcher过滤器,以便加Spring容器 -->
<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> <!-- 指明spring配置文件在何处 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring*.xml</param-value>
</context-param> <!-- 开启监听, 加载spring配置文件applicationContext.xml -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<session-config>
<session-timeout>1</session-timeout>
</session-config> <!-- web欢迎界面 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> </web-app>

web.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>
<!-- 需要Spring时添加 -->
<!-- <constant name="struts.objectFactory" value="spring" /> --> <package name="default" extends="struts-default">
<action name="Login" class="loginAction" method="execute">
<result name="success">index.jsp</result>
<result name="input">login.jsp</result>
</action>
</package> </struts>

struts.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-3.0.xsd"> <!--
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://127.0.0.1:1433;databaseName=springTest">
</property>
<property name="username" value="sa"></property>
<property name="password" value="111111"></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.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/HYOpticalComm/model/Login.hbm.xml</value>
</list>
</property>
</bean>
-->
<!--
<import resource="config/spring/spring-basic.xml" />
<import resource="config/spring/spring-dao.xml" />
-->
<import resource="config/spring/spring-service.xml" />
<import resource="config/spring/spring-action.xml" />
</beans>

spring.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-3.0.xsd"> <bean id="loginAction" class="com.HYOpticalComm.action.LoginAction">
<property name="loginService" ref="loginServiceImp"></property>
</bean>
</beans>

spring-action.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-3.0.xsd"> <bean id="loginServiceImp" class="com.HYOpticalComm.serviceImp.LoginServiceImp" />
</beans>

spring-service.xml

<%@ 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>
</head> <body> <s:form action="Login" method="post">
<s:textfield key="username" label="用户名" />
<s:password key="password" label="密码" />
<s:submit value="登陆" />
</s:form> </body>
</html>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head> <body>
<div>
<h4>欢迎你!</h4><font color="red">${username}</font>
<br/>
<h4>你登录的密码是<h4><font color="red">${password}</font>;
</div>
</body>
</html>

index.jsp

package com.HYOpticalComm.action;

import com.HYOpticalComm.service.ILoginService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{ /**
*
*/
private static final long serialVersionUID = 1L; private String username;
private String password; /*
* 我们通过Spring的IOC容器注入LoginService,从而减少类之间的依赖关系
*/
private ILoginService loginService; public ILoginService getLoginService()
{
return loginService;
}
public void setLoginService(ILoginService loginService)
{
this.loginService = loginService;
}
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;
} @Override
public void validate()
{
/*
* 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证
*/
} @Override
public String execute() throws Exception
{ boolean result = loginService.validate(username, password);
if(result == true)
{
return SUCCESS;
}
else
{
return INPUT;
}
}
}

LoginAction.java

package com.HYOpticalComm.service;

/*接口类    */
public interface ILoginService
{
public boolean validate(String username,String password)throws Exception;
}

ILoginService.java

package com.HYOpticalComm.serviceImp;

import com.HYOpticalComm.service.ILoginService;

public class LoginServiceImp implements ILoginService
{
/*
* 我们这只是一个小的例子,不与数据库打交到
* 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作
*/
public boolean validate(String username,String password)throws Exception
{
boolean v = false;
if(!"admin".equals(username))//如果用户名不等于xcp,就抛出一个异常
{
//throw new UsernameException("用户名不正确");
}
else if(!"123".equals(password))//如果密码不等于123,就抛出一个异常 {
//throw new PasswordException("密码不正确");
}
else
{
v = true;
}
return v;
}
}

LoginServiceImp.java

SSH框架(四) struts2+spring3.0的登陆示例的更多相关文章

  1. SSH (Struts2+Spring3.0+Hibernate3)框架(二) 框架的配置

    一.准备工作: 1. JDK -> jdk1.6.0_17 安装(环境变量配置): JAVA_HOME = C:\ jdk1.6.0_17; PATH = %JAVA_HOME%\bin; %J ...

  2. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤

    在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...

  3. SSH框架之Struts2第一篇

    1.2 Struts2的概述 : Struts2是一个基于MVC设计模式的WEB层的框架. 1.2.1 常见web层框架 Struts1,Struts2,WebWork,SpringMVC Strut ...

  4. 搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...

  5. SSH框架总结(帧分析+环境结构+示例源代码下载)

    首先,SSH不是一个框架.而是多个框架(struts+spring+hibernate)的集成,是眼下较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...

  6. SSH (Struts2+Spring3.0+Hibernate3)框架(一) 理论

    典型的J2EE三层结构,分为表现层.中间层(业务逻辑层)和数据服务层.三层体系将业务规则.数据访问及合法性校验等工作放在中间层处理.客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与 ...

  7. ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例

    一 . 需求 学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅! 本文将记录整合struts + spring的过程 , ...

  8. SSH框架之Struts2第三篇

    1.3相关知识点 : 1.3.1 OGNL的表达式 : 1.3.1.1 什么是OGNL OGNL是Object-Graph Navigation Language的编写,它是一种功能强大的表达式语言, ...

  9. SSH框架学习------struts2(一)

    1.总的目录 2.所有程序 1)index.jsp很简单 <%@ page language="java" contentType="text/html; char ...

随机推荐

  1. HAWQ 官方文档创建filespace,tablespace,database,table

    1.创建Filespace 创建Filespace必须是数据库超级用户( You must be a database superuser to create a filespace.)首先创建一个f ...

  2. 七、python沉淀之路--集合

    一. 1.字符串转集合 s = 'hello' se = set(s) print(se) {'e', 'o', 'h', 'l'} 2.列表转集合 l1 = ['hello','python','n ...

  3. CentOS7中配置基于Nginx+Supervisor+Gunicorn的Flask项目

    配置Nginx 1.安装nginx yum install nginx 2.安装好后在/etc/nginx/default.d中添加location的配置,并指向8001端口,以后Gunicorn会监 ...

  4. Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之验证码

    验证码这东西,有人喜欢有人不喜欢.对于WebApi是否需要验证码,没去研究过,只是原来的SimpleCMS有,就加上吧. 在WeiApi上使用验证码,关键的地方在于WeiApi是没有状态的,也就是说, ...

  5. Day3(1)linux文件系统及文件类型

    Linux的文件系统 根文件系统(rootfs) root filesystem LSB,FHS:(FileSystem Heirache Standard) /etc,/usr,/var,/root ...

  6. java写出进程条代码

    package com.ds; import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import ...

  7. AllowsTransparency和WebBrowser兼容性问题解决方案

    AllowsTransparency和System.Windows.Controls.WebBrowser兼容性问题,能看这篇文章,所以原因也不用多说:最根本的就是因为MS对win32底层的WebBr ...

  8. linux命令-su切换用户

    查看当前用户 #id uid=0(root) gid=0(root) 组=0(root) #whoami root ////////////////////////////////////////// ...

  9. java之类和对象

    类的成员: 成员变量和成员函数. 成员函数:构造函数和普通函数. 构造函数: 作用:自动对对象进行初始化 特点:1.方法名和类名一致 2.没有返回值 问: 1.我们能够定义几次构造函数? 我们可以定义 ...

  10. from xml.etree import cElementTree as ET