Struts+springの例1(北京尚学堂教程より)


Spring+strutsの統合(第1の統合スキーム)
Spring+strutsの統合(第1の統合スキーム)
原理:ActionでBeanFactoryオブジェクトを取得し、BeanFactoryでビジネスロジックオブジェクトを取得する
1、spring、struts依存ライブラリ構成
*strutsの構成
--strutsクラスライブラリとjstlクラスライブラリのコピー
--Webを変更します.xmlファイルでActionServiceletを構成する
--struts-configを提供する.xmlファイル
--国際化されたリソースファイルの提供
*springの構成
--springクラスライブラリのコピー
--springプロファイルの提供
2、strutsのActionで次のコードを呼び出してBeanFactoryを取得する
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
3.BeanFactoryでビジネスオブジェクトを取得し、ビジネスロジックメソッドを呼び出す
 
 
WEB-INF下
 
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <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>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>  
 
 <!--  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext-*.xml</param-value>
  </context-param>
 -->
   <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:applicationContext-*.xml</param-value>
  </context-param>
   
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
   
</web-app>

 
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="loginForm" type="com.bjsxt.usermgr.forms.LoginActionForm"/>
	</form-beans>
	
	<action-mappings>
		
		<action path="/logininput"
				forward="/login.jsp"
		></action>
	
		<action path="/login"
				type="com.bjsxt.usermgr.actions.LoginAction"
				name="loginForm"
				scope="request"	
		>
			<forward name="success" path="/success.jsp"/>
		</action>
	</action-mappings>

    <message-resources parameter="MessageResources" />
</struts-config>


 
WebContentまたはWebRootで
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>spring struts   (     )</title>
</head>
<body>
	<h1>spring struts   (     )</h1>
	<hr>
	<a href="logininput.do">  </a>
</body>
</html>
 
 
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>    </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>
 
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>
 
 
 
src下
applicationContext-beans.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:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<bean id="userManager" class="com.bjsxt.usermgr.manager.UserManagerImpl"/>
</beans>
 
MessageResources.properties
# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Blank Application
welcome.heading=Welcome!
welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)
 
 
 
src.com.bjsxt.usermgr.Actionsの下でjava
 
package com.bjsxt.usermgr.actions;

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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.bjsxt.usermgr.forms.LoginActionForm;
import com.bjsxt.usermgr.manager.UserManager;
import com.bjsxt.usermgr.manager.UserManagerImpl;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm laf = (LoginActionForm)form;
		
		//UserManager userManager = new UserManagerImpl();
		//userManager.login(laf.getUsername(), laf.getPassword());
		
		//BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml");
		//UserManager userManager = (UserManager)factory.getBean("userManager");
		//userManager.login(laf.getUsername(), laf.getPassword());
		
		BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		
		//ApplicationContext pc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		UserManager userManager = (UserManager)factory.getBean("userManager");
		userManager.login(laf.getUsername(), laf.getPassword());
		
		return mapping.findForward("success");
	}

	
}
 
src.com.bjsxt.usermgr.formsの下でLoginActionForm.java
package com.bjsxt.usermgr.forms;

import org.apache.struts.action.ActionForm;

public class LoginActionForm extends ActionForm {
	
	private String username;
	
	private String password;

	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;
	}
}
 
src.com.bjsxt.usermgr.管理下
UserManager.java
package com.bjsxt.usermgr.manager;

public interface UserManager {

	public void login(String username, String password);
}
 
UserManagerImpl.java
package com.bjsxt.usermgr.manager;

public class UserManagerImpl implements UserManager {

	public void login(String username, String password) {
		System.out.println("UserManagerImpl.login() -- username=" + username);
	}
}