[Spring MVC] example

Spring MVC Example

download

Ref: http://snoopy81.tistory.com/241

c.f. rename attached file extention to 7z

We show Spring MVC example. From this, you will know how to make spring mvc code.

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>myHome</display-name>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>myHome01</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>myHome01</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>

Description

8: define listener for web application

13~14: you can define context configuration file like them.

19~21: define servlet for myHome01. ‘load-on-startup’ tag means loading at startup time not request time. Nomally this does when the servlet is heavy, so response fastly at requesting.

24~27: defne servlet mapping. *.html url request go to myHome01 servelet. and myHome1 servlet process this request.

myHome01-servlet.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- HandlerMapping-->
	<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/board.html">indexController</prop>
				<prop key="/detail.html">detailController</prop>
			</props>
		</property>
	</bean>

	<!-- ViewResolver-->
	<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass">
			<value>org.springframework.web.servlet.view.JstlView</value>
		</property>
		<property name="prefix">
			<value>WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>

	<!-- bean -->
	<bean id="indexController" class="controller.IndexController">
		<property name="board" ref="board"></property>
	</bean>

	<bean id="detailController" class="controller.DetailController">
		<property name="board" ref="board"></property>
	</bean>

	<bean id="board" class="logic.BoardImpl">
		<property name="boardDao" ref="boardDao"></property>
	</bean>

	<bean id="boardDao" class="dao.BoardDaoImpl">
		<property name="sqlMapClient" ref="sqlMapClient"></property>
	</bean>

</beans>

*SimpleUrlHanderMapping Class
-mapping request url to controller

*InternalResourceViewResolver Class
-Set viewClass, prefix, suffix properties
-viewClass: Define class that implement view interface
==>JstlView class: support JSP page using JSTL

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<settings useStatementNamespaces="true"/>
<sqlMap resource="../classes/sql/oracle/board.xml"/>
</sqlMapConfig>

* for using iBatis

board.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="board">

	<select id="getBoardList" resultClass="vo.BoardVO">
		SELECT 	BOARD_IDX 						AS BOARDIDX,
				BOARD_SEQ 						AS BOARDSEQ,
				BOARD_TYPE 						AS BOARDTYPE,
				STEP							AS STEP,
				TITLE							AS TITLE,
				CONTENT							AS CONTENT,
				TO_CHAR(REGI_DT, 'YYYY-MM-DD') 	AS REGIDT,
				REGI_USER 						AS REGIUSER,
				TO_CHAR(MODI_DT, 'YYYY-MM-DD') 	AS MODIDT,
				MODI_USER 						AS MODIUSER
		FROM 	BOARD
		ORDER BY BOARD_IDX DESC, BOARD_SEQ DESC
	</select>

		<select id="getBoardDetail" parameterClass="int" resultClass="vo.BoardVO">
		SELECT 	BOARD_IDX 						AS BOARDIDX,
				BOARD_SEQ 						AS BOARDSEQ,
				BOARD_TYPE 						AS BOARDTYPE,
				STEP							AS STEP,
				TITLE							AS TITLE,
				CONTENT							AS CONTENT,
				TO_CHAR(REGI_DT, 'YYYY-MM-DD') 	AS REGIDT,
				REGI_USER 						AS REGIUSER,
				TO_CHAR(MODI_DT, 'YYYY-MM-DD') 	AS MODIDT,
				MODI_USER 						AS MODIUSER
		FROM 	BOARD
		WHERE 	BOARD_IDX = #value#
	</select>

</sqlMap>

 

board.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>BOARD LIST</title>

<script type="text/javascript">
	function goDetail(board_idx){
		var doc = document.boardForm;
		doc.boardIdx.value = board_idx;
		//alert(board_idx);
		doc.action = "./detail.html";
		doc.submit();
	}
</script>

</head>
<body>
<form name="boardForm">
	<input type="hidden" name="boardIdx" value=""/>
	<table border="1">
		<tr>
			<th width="50">No.</th>
			<th width="400">제목</th>
			<th width="100">등록일</th>
			<th width="100">등록자</th>
		</tr>
	<c:forEach items="${boardList}" var="board">
		<tr>
			<td align="center"> <c:out value="${board.boardIdx}"></c:out> </td>
			<td align="left" onclick="goDetail('<c:out value="${board.boardIdx}"></c:out>')"> <c:out value="${board.title}"></c:out> </td>
			<td align="center"> <c:out value="${board.regiDt}"></c:out> </td>
			<td align="center"> <c:out value="${board.regiUser}"></c:out> </td>
		</tr>
	</c:forEach>
	</table>
</form>
</body>
</html>

detail.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>BOARD LIST</title>
</head>
<body>

	<table border="1">
		<tr>
			<th width="50">No.</th>
			<th width="400">제목</th>
			<th width="100">등록일</th>
			<th width="100">등록자</th>
		</tr>

		<tr>
			<td align="center"> <c:out value="${boardDetail.boardIdx}"></c:out> </td>
			<td align="left"> <c:out value="${boardDetail.title}"></c:out> </td>
			<td align="center"> <c:out value="${boardDetail.regiDt}"></c:out> </td>
			<td align="center"> <c:out value="${boardDetail.regiUser}"></c:out> </td>
		</tr>

	</table>

	<input type="button" value=" list " onclick="window.location='board.html'" />

</body>
</html>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s