[Spring] AbstractController

* AbstractController

– Abstract Class implemented Controller interface

– Return ModelAndView after process client’s request

– In controller inherited AbstractController, it only requires to implement handleRequestInternal method.

<?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="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
         <property name="mappings">
             <props>
                 <prop key="/listAbsCont.sp">listAbstractController</prop>
             </props>
         </property>
     </bean>

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

    <bean id="listAbstractController" class="controller.ListAbstractController">
        <property name="supportedMethods" value="GET"></property>
    </bean>

</beans>

 

package controller;

import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.servlet.mvc.AbstractController;

public class ListAbstractController extends AbstractController {

    @Override
     protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        //parameter
         String param1 = request.getParameter("param1");

        //implement your require

        //ModelAndView
         ModelAndView modelAndView = new ModelAndView();
         modelAndView.setViewName("view_name");
         modelAndView.addObject("aaa", "bbb");

        return modelAndView;
     }
 }

Properties of AbstractController

Property Default Description
supportedMethod GET, POST HTTP method that conroller support
requirSession false Set whether require HTTPsession for processing request.
Exception occures when HttpSession doesn’t exist
cacheSeconds -1 cache header value for response in unit – second
0 – header doesn’t have  executing cache
-1 – doen’t have cache related header.
> 0 – include corresponding value in cache header

[Spring] Kind of Controller

Spring kind of controller

Purpose Class(Interface) Description
Simple Process Controller
AbstractController
you do to implement required feature
parameter mapping AbstractCommandController Save request parameter to object and validate it
Process input form SimpleFormController Print form and process input data
Process multi page input form AbstractWizardFormController input data through multi page
Controll input form’s flow and process input data
Mapping static view ParameterizableViewController

UrlFilenameViewController

Not process any function in controller, simply
pass client’s request to view
Multi action MultiActionController process relation function or logic in one controller

[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>

[jQuery] basic syntax

selector

• $(‘p’)—Accesses all the paragraph elements in the HTML file
• $(‘div’)—Accesses all the div elements in the HTML file
• $(‘#A’)—Accesses all the HTML elements with id=A
• $(‘.b’)—Accesses all the HTML elements with

addClass()
This method is used for applying a CSS class to the selected part of the page.

prepend()
This method inserts the specified content at the beginning of the selected element and returns a jQuery
object. The content can be in the form of text, an HTML element, or a jQuery object.
Let’s insert an h2 element with the text Power of selectors before the paragraph element. The jQuery
code for doing so is as follows:
$(‘p’).prepend(‘<h2> Power of selectors </h2>’);

prependTo()
Similar to prepend(), prependTo() is used for adding DOM nodes dynamically. It inserts the specified
element(s) at the beginning of the selected target, where the target can be in the form of an HTML
element, a string, or a jQuery object. The method returns a jQuery object. The following is the equivalent
of the previous prepend() call.
$(‘<h2> Power of selectors </h2>’).prependTo(‘p’);
We can see that the contents we want to insert (the h2 element) precede the method.

clone()
When we want to add a DOM node (which is a copy of an existing element) on the fly, we use the clone()
method. This method makes a copy of the selected element and returns it as a new jQuery object. To
make a copy of the h2 element in the jQuery code and insert it before the paragraph element, the code
may be as follows:
$(‘h2’).clone().prependTo(‘p’);