SSH框架(四) struts2+spring3.0的登陆示例
(一)关键理念及需要注意的地方:
使用struts2+spring3.0的框架搭建web程序,就是使用spring来进行依赖注入(依赖注入请参考baidu上面的解释:http://baike.baidu.com/link?url=uESWlODOsyqaaqlGLxps8xh2UaadfEe2rdsjspvZN5qsw1BOitPx_QQYuPV904jCwb493WK1ROrO3iIPZrbAQa)。
需要注意的地方如下:
- struts.xml中的action的class要写spring中的bean,这个意思就是让spring去实例化改对象。比如:<action name="Login" class="loginAction" method="execute">
- 在action的bean中在引入相关的类
<bean id="loginAction" class="com.HYOpticalComm.action.LoginAction">
<property name="loginService" ref="loginServiceImp"></property> ---这里边调用的是LoginAction类的setter函数。并使用loginServiceImp类来实例化
</bean> - 要包含“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的登陆示例的更多相关文章
- SSH (Struts2+Spring3.0+Hibernate3)框架(二) 框架的配置
一.准备工作: 1. JDK -> jdk1.6.0_17 安装(环境变量配置): JAVA_HOME = C:\ jdk1.6.0_17; PATH = %JAVA_HOME%\bin; %J ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤
在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...
- SSH框架之Struts2第一篇
1.2 Struts2的概述 : Struts2是一个基于MVC设计模式的WEB层的框架. 1.2.1 常见web层框架 Struts1,Struts2,WebWork,SpringMVC Strut ...
- 搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...
- SSH框架总结(帧分析+环境结构+示例源代码下载)
首先,SSH不是一个框架.而是多个框架(struts+spring+hibernate)的集成,是眼下较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...
- SSH (Struts2+Spring3.0+Hibernate3)框架(一) 理论
典型的J2EE三层结构,分为表现层.中间层(业务逻辑层)和数据服务层.三层体系将业务规则.数据访问及合法性校验等工作放在中间层处理.客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与 ...
- ssh框架整合---- spring 4.0 + struts 2.3.16 + maven ss整合超简单实例
一 . 需求 学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅! 本文将记录整合struts + spring的过程 , ...
- SSH框架之Struts2第三篇
1.3相关知识点 : 1.3.1 OGNL的表达式 : 1.3.1.1 什么是OGNL OGNL是Object-Graph Navigation Language的编写,它是一种功能强大的表达式语言, ...
- SSH框架学习------struts2(一)
1.总的目录 2.所有程序 1)index.jsp很简单 <%@ page language="java" contentType="text/html; char ...
随机推荐
- 【LeetCode】001. TwoSum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [转]angular的路由机制
在谈路由机制前有必要先提一下现在比较流行的单页面应用,就是所谓的single page APP.为了实现无刷新的视图切换,我们通常会用ajax请求从后台取数据,然后套上HTML模板渲染在页面上,然而a ...
- xj监控端口,模拟登陆脚本
#!/bin/bash date=`date +%Y%m%d-%H%M` count=0 ip1=124.117.246.195 ip2=124.117.246.194 port1=(443 80 6 ...
- Erlang pool management -- RabbitMQ worker_pool 2
上一篇已经分析了rpool 的三个module , 以及简单的物理关系. 这次主要分析用户进程和 worker_pool 进程还有worker_pool_worker 进程之间的调用关系. 在开始之前 ...
- JSP/java 执行创建批处理文件,并执行批处理事务。
protected void doGet(HttpServletRequest req, HttpServletResponse resp) { InputStream in = null; Inpu ...
- 蓝桥杯 算法训练 ALGO-139 s01串
算法训练 s01串 时间限制:1.0s 内存限制:256.0MB 问题描述 s01串初始为”0” 按以下方式变换 0变1,1变01 输入格式 1个整数(0~19) 输出格式 n次变换后s01 ...
- 机器学习:PCA(基础理解、降维理解)
PCA(Principal Component Analysis) 一.指导思想 降维是实现数据优化的手段,主成分分析(PCA)是实现降维的手段: 降维是在训练算法模型前对数据集进行处理,会丢失信息. ...
- Sass和Less、Stylus的转译和语法(1)
四.Sass.LESS和Stylus转译成CSSSass.LESS和Stylus源文件(除了LESS源文件在客户端下运行之外)都不能直接被浏览器直接识别,这样一来,要正常的使用这些源文 件,就需要将其 ...
- 执行: python manage.py makemigrations报AttributeError: 'str' object has no attribute 'decode'
找到错误代码(line146):query = query.encode(errors='replace') 解决方法:把decode改为encode即可.