spring+struts1

Posted 东北大亨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring+struts1相关的知识,希望对你有一定的参考价值。

 

概括及介绍:

  集成原理:在Action 中获得BeanFactory,通过BeanFactory取得业务逻辑对象

  本例采用:JDK1.8,tomcat7.0.9
  技术点:spring与strut1集成方案例子介绍
  

集成原理:在Action 中获得BeanFactory,通过BeanFactory取得业务逻辑对象

1、spring和struts1的依赖包管理

   * struts1 
   -- 拷贝struts1和jstl的依赖包
   -- 在web.xml文件中配置 ActionServlet
   -- 提供 struts-config.xml 文件
   -- 提供国际化支持,提供缺省国际化资源文件
   * spring
   -- 拷贝spring相关依赖包
   -- 提供spring的配置文件
   
 2、在web.xml文件中配置ContextLoaderListener,让web server	在启动的时候
   BeanFactory 放到serverletContext中提高效率。
   
	  <!-- 在web.xml中通过contextConfigLocation配置spring,
	  contextConfigLocation参数定义了要装入的 Spring 配置文件。  -->
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath:applicationContext1.xml</param-value>
	  </context-param>
	  <listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	  </listener>
3、 在action中采用 	 WebApplicationContextUtils.getRequiredWebApplicationContext() 从
  ServletContext 中取得BeanFactory。
4. BeanFactory从业务逻辑中取得对象

	存在缺点:
	  (主动查找存在问题,存在对象依赖。)Action中存在了依赖查找,所以action 依赖 spring 的 API。
	     进一步了解依赖查找和依赖注入

 

一、项目架构截图

     

       

           

 

 

二、代码接口介绍

     1、strut1中LoginActionForm 获得前台提交数据

     2、定义模拟验证登录接口:UserManager

     3、实现登录接口实现类:UserManagerImpl

     4、strut1的Action:LoginAction

     5、spring的配置文件:applicationContext1.xml

     6、strut1配置文件:struts-config.xml

     7、strut1中web.xml文件

     8、欢迎页:index.jsp

     9、登录页:login.jsp

    10、登录成功页:success.jsp

三、代码

     1、strut1中LoginActionForm 获得前台提交数据

     

package com.tycoon.usermanager.forms;

import org.apache.struts.action.ActionForm;

/**
 *  
 * LoginActionForm
 * @author northEastTycoon 
 */
public class LoginActionForm extends ActionForm {

	private static final long serialVersionUID = 1L;
	
	// 用户名称
	private String username="northEastTycoon";
	
	// 用户密码
	private String password="northEastTycoon";
	

	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;
	}
}

 

       2、定义模拟验证登录接口:UserManager

     

package com.tycoon.usermanager.manager;

/**
 *  
 * UserManager :模拟登陆接口
 * @author northEastTycoon 
 */
public interface UserManager {
	
	public void login(String username,String password);
}

     3、实现登录接口实现类:UserManagerImpl

  

package com.tycoon.usermanager.manager;


/**
 *  
 * UserManager :实现模拟登陆接口类
 * @author northEastTycoon 
 */
public class UserManagerImpl implements UserManager {

	@Override
	public void login(String username, String password) {
		System.out.println(this.getClass()+",username:"+username);

	}

}

   4、 strut1的Action:LoginAction

 

package com.tycoon.usermanager.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tycoon.usermanager.forms.LoginActionForm;
import com.tycoon.usermanager.manager.UserManager;

/**
 *  
 * Action类 :控制类
 * @author northEastTycoon 
 */
public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		LoginActionForm laf = (LoginActionForm) form;
		String username = laf.getUsername();
		String password = laf.getPassword();

		// 由于耗时,还可以从ServletContext获得BeanFactory对象 
		BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		UserManager userManager = (UserManager)factory.getBean("userManager");
		userManager.login(username, password);
		
		return mapping.findForward("success");
	}

}

    5、 spring的配置文件:applicationContext1.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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<bean id="userManager" class="com.tycoon.usermanager.manager.UserManagerImpl"/>
</beans>

      6、strut1配置文件:struts-config.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
 "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="loginForm"
			type="com.tycoon.usermanager.forms.LoginActionForm"></form-bean>
	</form-beans>
	<action-mappings>
		<action path="/login" type="com.tycoon.usermanager.web.action.LoginAction"
			name="loginForm" scope="request">
			<forward name="success" path="/success.jsp"></forward>
		</action>
	</action-mappings>
	<message-resources parameter="MessageResources" />
</struts-config>

 7、strut1中web.xml文件

   

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <context-param>
  <!-- 在web.xml中通过contextConfigLocation配置spring,
  contextConfigLocation参数定义了要装入的 Spring 配置文件。  -->
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext1.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

              8、欢迎页:index.jsp

   

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<h1>Spring+struts1(东北大亨实例)</h1>
	<hr>
	<hr>
	<a href="login.jsp">登录</a>
</body>
</html>

          9、登录页:login.jsp

  

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<form action="login.do" method="post">
		用户:<input type="text" name="username"><br> 密码:<input
			type="password" name="password"><br> <input
			type="submit" value="登录">
	</form>
</body>
</html>

 

              10、  登录成功页:success.jsp

  

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	${loginForm.username},用户登录成功!
</body>
</html>

 

 运行截图:

1、请求欢迎页面

    

2、点击登录,跳转到登录页面

   

3、显示成功后页面

  

 

以上是关于spring+struts1的主要内容,如果未能解决你的问题,请参考以下文章

struts1和struts2的区别??

struts2和strtus1的主要区别

struts1学习

Spring security + Struts 1.2 集成

jsf struts jsp spring 啥关系和区别?

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段