note

@PathVariable 을 이용한 주소 확장기능 본문

JSP/Spring

@PathVariable 을 이용한 주소 확장기능

투한 2012. 3. 6. 10:30






key와 value값이 /로 표현되어짐










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

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 요청 URI 매칭 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="alwaysUseFullPath" value="true" />
	</bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="alwaysUseFullPath" value="true" />
	</bean>
	
	<bean class="madvirus.spring.chap06.controller.GameInfoController" />
	
	
	<!-- @PathVariable 어노테이션을 이용한 URI 템플릿 -->
	<bean class="madvirus.spring.chap06.controller.CharacterInfoController" />
	
	<!-- View 글로벌 설정 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 리소스 번들 지정 -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	 <property name="basenames">
	 	<list>
	 		<value>messages.validation</value>
	 	</list>
	 </property>
	</bean>
</beans>




CharacterInfoController.java
package madvirus.spring.chap06.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CharacterInfoController {

	@RequestMapping("/game/users/{userId}/characters/{characterId}")
	public String characterInfo(@PathVariable String userId,
			@PathVariable int characterId, ModelMap model) {
		model.addAttribute("userId",userId);
		model.addAttribute("characterId",characterId);
		return "game/character/info";
	}
}




game/character/info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>캐릭터 정보</title>
</head>
<body>
${userId}회원의 ${characterId} 캐릭터 정보 출력
</body>
</html>