有限会社 ドルフィン


Struts1 ↔ SpringMVC
Struts1 SpringMVC
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/javaee" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	id="WebApp_ID" version="2.5">						
  <display-name>mvc-struts</display-name>						
  <description>Spring MVC と Struts サンプル</description>						
  <context-param>						
    <param-name>webAppRootKey</param-name>						
    <param-value>mvc-struts.root</param-value>						
  </context-param>						
  <context-param>						
    <param-name>contextConfigLocation</param-name>						
    <param-value>/WEB-INF/applicationContext.xml</param-value>						
  </context-param>						
  <listener>						
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>						
  </listener>						
  <servlet>						
    <servlet-name>action</servlet-name>						
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>						
    <load-on-startup>3</load-on-startup>
  </servlet>						
<servlet-mapping>						
	<servlet-name>action</servlet-name>					
	<url-pattern>*.do</url-pattern>					
</servlet-mapping>						
  <welcome-file-list>						
    <welcome-file>index.html</welcome-file>						
  </welcome-file-list>						
</web-app>						

<?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/javaee" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	id="WebApp_ID" version="2.5">						
  <display-name>mvc-struts</display-name>								
  <description>Spring MVC と Struts サンプル</description>								
  <context-param>								
    <param-name>webAppRootKey</param-name>								
    <param-value>mvc-struts.root</param-value>								
  </context-param>								
  <context-param>								
    <param-name>contextConfigLocation</param-name>								
    <param-value>/WEB-INF/applicationContext.xml</param-value>								
  </context-param>								
  <listener>								
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>								
  </listener>								
								
  <servlet>								
    <servlet-name>mvc-struts</servlet-name>								
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>								
    <load-on-startup>2</load-on-startup>					
  </servlet>								
<servlet-mapping>								
	<servlet-name>mvc-struts</servlet-name>							
	<url-pattern>*.do</url-pattern>							
</servlet-mapping>								
  <welcome-file-list>								
    <welcome-file>index.html</welcome-file>								
  </welcome-file-list>								
</web-app>								

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: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.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="getListService" class="org.spring.service.GetListServiceImpl" />
</beans>		
struts-config.xml mvc-struts-servlet.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
 <form-beans>							
 </form-beans>							

 <global-forwards>			
  <forward name="failure" 
     path="/WEB-INF/jsp/Error.jsp" redirect="false"/>
 </global-forwards>							

 <action-mappings>							
    <action path="/getListdisp" 
       type="org.spring.struts.ListDispAction"						
       scope="session" validate="false">				
       <forward name="success"
          path="/WEB-INF/jsp/listView.jsp"/>
    </action>						
 </action-mappings>							
</struts-config>
<?xml version="1.0" encoding="UTF-8" ?>	
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
  "http://www.springframework.org/dtd/spring-beans-2.0.dtd">	
	
<beans>	
	
 <bean id="viewResolver"
     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="viewClass"
       value="org.springframework.web.servlet.view.JstlView"/>
   <property name="prefix" value="/WEB-INF/jsp/"/>	
   <property name="suffix" value=".jsp"/>			
 </bean>

 <bean id="defaultHandlerMapping" 
   class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

 <bean name="/getListdisp.do" class="org.spring.mvc.ListDispController">
   <property name="getListService" ref="getListService"/>
 </bean>

</beans>
BaseAction.java ListDispController.java
public abstract class BaseAction extends Action {
  /** サービスクラス*/
  private GetListService getListService;

  public void setServlet(ActionServlet actionServlet) {
    super.setServlet(actionServlet);
    if (actionServlet != null) {
      ServletContext sc = actionServlet.getServletContext();
      WebApplicationContext wac =
         WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
      this.getListService = (GetListService) wac.getBean("getListService");
    }
  }

  public GetListService getGetListService() {
    return getListService;
  }
  public void setGetListService(GetListService getListService) {
    this.getListService = getListService;
  }
}
public class ListDispController implements Controller {    
  /** サービスクラス*/  
  private GetListService getListService;  
    
  public ModelAndView handleRequest(HttpServletRequest request, 
                          HttpServletResponse response) throws Exception {  
    String keyword = request.getParameter("keyword");
    if (!StringUtils.hasLength(keyword)) {
      return new ModelAndView("Error", "message", "keyword Error.");
    }
    ArrayList list = getGetListService().getListService(1);
    return new ModelAndView("listView", "listData", list);
  }  
    
  public GetListService getGetListService() {  
    return getListService;
  }  
  public void setGetListService(GetListService getListService) {  
    this.getListService = getListService;
  }  
}
ListDispAction.java
public class ListDispAction  extends BaseAction {  
  
  @Override
  public ActionForward execute(ActionMapping mapping, ActionForm form, 
          HttpServletRequest request, HttpServletResponse response) 
          throws Exception {
    String keyword = request.getParameter("keyword");
    if (!StringUtils.hasLength(keyword)) {
      request.setAttribute("message", "keyword Error.");    
      return mapping.findForward("failure");
    }
    ArrayList list = getGetListService().getListService(1);
    request.setAttribute("listData", list);
    return mapping.findForward("success");
  }
}  
GetListService.java
package org.spring.service;
import java.util.ArrayList;
public interface GetListService {
	public abstract ArrayList getListService(int ey);
}
GetListServiceImpl.java
package org.spring.service;
import java.util.ArrayList;
public class GetListServiceImpl implements GetListService {
	@Override
	public ArrayList getListService(int ey) {
		ArrayList list = new ArrayList();
		ListRow row = null;
		row = new ListRow();
		row.setId("1");
		row.setName("user1");
		row.setTel("000111121");
		list.add(row);
		row = new ListRow();
		row.setId("2");
		row.setName("user2");
		row.setTel("111000132");
		list.add(row);
		row = new ListRow();
		row.setId("3");
		row.setName("user3");
		row.setTel("222000321");
		list.add(row);
		row = new ListRow();
		row.setId("4");
		row.setName("user4");
		row.setTel("333000124");
		list.add(row);
		row = new ListRow();
		row.setId("5");
		row.setName("user5");
		row.setTel("444000123");
		list.add(row);
		return list;

	}
}
ListRow.java
package org.spring.service;
public class ListRow {
	/** NO */
	private String id;
	/** 名前*/
	private String name;
	/** TEL*/
	private String tel;
	/**
	 * NOを取得します。
	 * @return NO
	 */
	public String getId() {
	    return id;
	}
	/**
	 * NOを設定します。
	 * @param id NO
	 */
	public void setId(String id) {
	    this.id = id;
	}
	/**
	 * 名前を取得します。
	 * @return 名前
	 */
	public String getName() {
	    return name;
	}
	/**
	 * 名前を設定します。
	 * @param name 名前
	 */
	public void setName(String name) {
	    this.name = name;
	}
	/**
	 * TELを取得します。
	 * @return TEL
	 */
	public String getTel() {
	    return tel;
	}
	/**
	 * TELを設定します。
	 * @param tel TEL
	 */
	public void setTel(String tel) {
	    this.tel = tel;
	}

}
listView.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>一覧</title>
</head>
<body>
	<table>
		<thead>
			<tr>
				<th>ID</th>
				<th>Name</th>
				<th>Tel</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach var="varList" items="${listData}">
				<tr>
  					<td>
  						<c:out value="${varList.id}"/>
  					</td>
  					<td>
  						<c:out value="${varList.name}"/>
  					</td>
  					<td>
  						<c:out value="${varList.tel}"/>
  					</td>
  				</tr>
			</c:forEach>
		</tbody>
	</table>
</body>
</html>

▲ページ先頭へ