Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- 에러페이지
- Menu
- 예외처리
- OGNL
- Java
- 어노테이션
- Android
- struts2
- HTML
- JSP
- 클래스
- layout
- paint
- 기본
- Spring
- Graphic
- 안드로이드
- Eclips
- 국제화
- 오버로딩
- oracle
- mybatis
- 메소드
- 이클립스
- 메서드
- AWT
- JavaScript
- 전화걸기
- 생성자
- 배열
Archives
- Today
- Total
note
Spring RequsetMapping의 부가적인 기능 컨트롤러 메서드의 HTTP 전송방식 한정 본문
파일 위치
[JSP/Spring] - Spring Web
이전 게시물
Command 클래스가 자바빈과 같다
방식과 요청을 따로 지정하기 위해 RequestMapping을 클래스에도 지정
NewArticleController
Form이 있는경우에 코딩을 이러한 방식으로 한다
@ModelAttribute("command") newArticleCommand 라는 것은 알아서 객체 생성이 된걸 알아서 넣어준다
그래서 jsp 파일에서 호출이 잘된다 EL 표기법으로
설정이 자동으로 처리한 방식이라서 더욱 진보된 방식이라함
앞에 /가 없는 이유는
글로벌 설정 dispatcher-servlet.xml에서
/WEB-INF/view/
view에 마지막에 / 가 있어서 명시를 안한다
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">
<!-- 기본 설정 -->
<!-- <servlet-name>dispatcher</servlet-name>을 써놓고 dispatcher-servlet.xml
의 앞에가 일치하면 자동으로 읽음
-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
<url-pattern>/game/*</url-pattern>
</servlet-mapping>
<!-- 캐릭터 인코딩 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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">
<context:annotation-config />
<!-- 기본 설정 -->
<bean id="helloController" class="madvirus.spring.chap06.controller.HelloController" />
<!-- 데이터를 전송하여 자바빈에 담기 -->
<bean id="newArticleController" class="madvirus.spring.chap06.controller.NewArticleController" />
<bean id="articleService" class="madvirus.spring.chap06.service.ArticleService" />
<!-- View 글로벌 설정 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
<context:annotation-config />
Autowired 어노테이션 사용을 위한 설정
NewArticleController.java
package madvirus.spring.chap06.controller;
import madvirus.spring.chap06.service.ArticleService;
import madvirus.spring.chap06.service.NewArticleCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//컨트롤러 설정
@Controller
//클라이언트의 요청에 대해서 호출하는 메서드 지정
@RequestMapping("/article/newArticle.do")
public class NewArticleController {
@Autowired
private ArticleService articleService;
@RequestMapping(method = RequestMethod.GET)
public String from(){
//view 설정
return "article/newArticleForm";
}
@RequestMapping(method = RequestMethod.POST)
//Command(자바빈) 클래스 설정
public String submit(@ModelAttribute("command") NewArticleCommand command){
articleService.writeArticle(command);
//view 설정
return "article/newArticleSubmitted";
}
public void setArticleService(ArticleService articleService){
this.articleService = articleService;
}
}
NewArticleCommand.java
package madvirus.spring.chap06.service;
public class NewArticleCommand {
private String title;
private String content;
private int parentId;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
@Override
public String toString() {
return "NewArticleCommand [content=" + content + ", parentId="
+ parentId + ", title=" + title + "]";
}
}
WEB-INF/view/article/newArticleForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>게시글 쓰기</title> </head> <body> 게시글 쓰기 입력 폼: <form method="post"> <input type="hidden" name="parentId" value="0" /> 제목: <input type="text" name="title" /><br/> 내용: <textarea name="content"></textarea><br/> <input type="submit" /> </form> </body> </html>
WEB-INF/view/article/newArticleSubmitted.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>
게시글 등록됨 : <br/>
제목 : ${command.title}
</body>
</html>
추가 내용 3/16일
ArticleService.java
package madvirus.spring.chap06.service;
public class ArticleService {
public void writeArticle(NewArticleCommand command) {
System.out.println("신규 게시글 등록: " + command);
}
}
'JSP > Spring' 카테고리의 다른 글
| Spring Servlet import 에러시 (0) | 2012.03.05 |
|---|---|
| @RequestParam 어노테이션을 이용한 파라미터 매핑 (0) | 2012.03.05 |
| Spring Web (1) | 2012.03.02 |
| Spring AOP XML스키마 기반의 POJO 클래스를 이용한 AOP 구현 (0) | 2012.03.02 |
| Spring AOP (1) | 2012.03.02 |