기본 세팅 프로젝트 파일
=================================이론=====================================
P309
뷰 영역 구현
P260
에러코드의 출력
P361
파일다운로드처리
p373~378
국제화처리
p362~367
엑셀,pdf 파일다운로드
===============================프로젝트===================================
chap07
src
madvirus.spring.chap07.controller
AuthenticationException *1 - 로그인 테스트 뷰영역 구현 에러코드출력
Authenticator *1
DownloadController *2 - 파일다운로드
LocaleChangeController *3 - p376 Locale resolver
LoginCommandValidator *1
LoginCommnad *1
LoginController *1
MockAuthenticator *1
PageRank *4 - 엑셀파일
PageRanksController *4 - 엑셀파일 다운로드
PageReportController *4 - pdf파일
madvirus.spring.chap07.view
DownloadView *2
PageRanksView *4 - 엑셀파일
PageReportView *4 - pdf파일
messages
label_en.properties *1
label.properties *1
validation_en.properties *1
validation.properties *1
WebContent
viewjsp
loginForm.jsp *1
loginSuccess.jsp *1
dispatcherInternal-servlet.xml *1
dispatcherNonHtml-servlet.xml *2
web.xml
index.jsp
=================================코드=====================================
chap07
src
madvirus.spring.chap07.controller
AuthenticationException *1 - 로그인 테스트 뷰영역 구현 에러코드출력
package madvirus.spring.chap07.controller;
public class AuthenticationException extends RuntimeException {
private static final long serialVersionUID = 1L;
public AuthenticationException(String message) {
super(message);
}
}
public class AuthenticationException extends RuntimeException {
private static final long serialVersionUID = 1L;
public AuthenticationException(String message) {
super(message);
}
}
Authenticator *1
package madvirus.spring.chap07.controller;
public interface Authenticator {
void authenticate(String id,String password);
}
public interface Authenticator {
void authenticate(String id,String password);
}
DownloadController *2 - 파일다운로드
package madvirus.spring.chap07.controller;
import java.io.File;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DownloadController implements ApplicationContextAware {
private WebApplicationContext context = null;
@RequestMapping("/file")
public ModelAndView download() throws Exception {
File downloadFile = getFile();
return new ModelAndView("download","downloadFile",downloadFile);
}
private File getFile(){
String path = context.getServletContext().getRealPath("/WEB-INF/설명.txt");
return new File(path);
}
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
this.context = (WebApplicationContext)applicationContext;
}
}
import java.io.File;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DownloadController implements ApplicationContextAware {
private WebApplicationContext context = null;
@RequestMapping("/file")
public ModelAndView download() throws Exception {
File downloadFile = getFile();
return new ModelAndView("download","downloadFile",downloadFile);
}
private File getFile(){
String path = context.getServletContext().getRealPath("/WEB-INF/설명.txt");
return new File(path);
}
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
this.context = (WebApplicationContext)applicationContext;
}
}
LocaleChangeController *3 - p376 Locale resolver
package madvirus.spring.chap07.controller;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.LocaleResolver;
@Controller
public class LocaleChangeController {
private LocaleResolver localeResolver;
@RequestMapping("/changLanguage")
public String change(@RequestParam("lang")String language,HttpServletRequest request, HttpServletResponse response){
Locale locale = new Locale(language);
localeResolver.setLocale(request, response, locale);
return "redirect:/index.jsp";
}
public void setLocaleResolver(LocaleResolver localeResolver){
this.localeResolver = localeResolver;
}
}
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.LocaleResolver;
@Controller
public class LocaleChangeController {
private LocaleResolver localeResolver;
@RequestMapping("/changLanguage")
public String change(@RequestParam("lang")String language,HttpServletRequest request, HttpServletResponse response){
Locale locale = new Locale(language);
localeResolver.setLocale(request, response, locale);
return "redirect:/index.jsp";
}
public void setLocaleResolver(LocaleResolver localeResolver){
this.localeResolver = localeResolver;
}
}
LoginCommandValidator *1
package madvirus.spring.chap07.controller;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class LoginCommandValidator implements Validator {
public void validate(Object target, Errors errors) {
//비어있거나 공백이있으면 돌려보냄
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "id", "required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "loginType", "required");
}
public boolean supports(Class<?> clazz) {
return LoginCommnad.class.isAssignableFrom(clazz);
}
}
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class LoginCommandValidator implements Validator {
public void validate(Object target, Errors errors) {
//비어있거나 공백이있으면 돌려보냄
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "id", "required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "loginType", "required");
}
public boolean supports(Class<?> clazz) {
return LoginCommnad.class.isAssignableFrom(clazz);
}
}
LoginCommnad *1
package madvirus.spring.chap07.controller;
public class LoginCommnad {
private String id;
private String password;
private String loginType;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getLoginType() {
return loginType;
}
public void setLoginType(String loginType) {
this.loginType = loginType;
}
}
public class LoginCommnad {
private String id;
private String password;
private String loginType;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getLoginType() {
return loginType;
}
public void setLoginType(String loginType) {
this.loginType = loginType;
}
}
LoginController *1
package madvirus.spring.chap07.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/*//web.xml 에서
<servlet-mapping>
<servlet-name>dispatcherInternal</servlet-name>
<url-pattern>/jsp/*</url-pattern>
</servlet-mapping> => /jsp/login/login.do */
@Controller
@RequestMapping("/login/login.do")
public class LoginController {
private Authenticator authenticator;
@ModelAttribute("login")
public LoginCommnad formBacking(){
return new LoginCommnad();
}
@RequestMapping(method = RequestMethod.GET)
public String form(){
return "loginForm";
}
@RequestMapping(method = RequestMethod.POST)
public String submit(@ModelAttribute("login") LoginCommnad loginCommand,BindingResult result){
new LoginCommandValidator().validate(loginCommand, result);
if(result.hasErrors()){
return "loginForm";
}
try{
authenticator.authenticate(loginCommand.getId(), loginCommand.getPassword());
return "loginSuccess";
}catch (AuthenticationException ex){ //null : loginCommand.getId() 가 null이면 default 값
result.reject("invalidIdOrPassword", new Object[] {loginCommand.getId()},null);
return "loginForm";
}
}
@ModelAttribute("loginTypes")
protected List<String> referenceData() throws Exception {
List<String> loginTypes = new ArrayList<String>();
loginTypes.add("일반회원");
loginTypes.add("기업회원");
loginTypes.add("헤드헌터회원");
return loginTypes;
}
public void setAuthenticator(Authenticator authenticator){
this.authenticator = authenticator;
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/*//web.xml 에서
<servlet-mapping>
<servlet-name>dispatcherInternal</servlet-name>
<url-pattern>/jsp/*</url-pattern>
</servlet-mapping> => /jsp/login/login.do */
@Controller
@RequestMapping("/login/login.do")
public class LoginController {
private Authenticator authenticator;
@ModelAttribute("login")
public LoginCommnad formBacking(){
return new LoginCommnad();
}
@RequestMapping(method = RequestMethod.GET)
public String form(){
return "loginForm";
}
@RequestMapping(method = RequestMethod.POST)
public String submit(@ModelAttribute("login") LoginCommnad loginCommand,BindingResult result){
new LoginCommandValidator().validate(loginCommand, result);
if(result.hasErrors()){
return "loginForm";
}
try{
authenticator.authenticate(loginCommand.getId(), loginCommand.getPassword());
return "loginSuccess";
}catch (AuthenticationException ex){ //null : loginCommand.getId() 가 null이면 default 값
result.reject("invalidIdOrPassword", new Object[] {loginCommand.getId()},null);
return "loginForm";
}
}
@ModelAttribute("loginTypes")
protected List<String> referenceData() throws Exception {
List<String> loginTypes = new ArrayList<String>();
loginTypes.add("일반회원");
loginTypes.add("기업회원");
loginTypes.add("헤드헌터회원");
return loginTypes;
}
public void setAuthenticator(Authenticator authenticator){
this.authenticator = authenticator;
}
}
MockAuthenticator *1
package madvirus.spring.chap07.controller;
public class MockAuthenticator implements Authenticator{
public void authenticate(String id, String password) {
if(!id.equals("madvirus")){
throw new AuthenticationException("invalid id"+ id);
}
}
}
public class MockAuthenticator implements Authenticator{
public void authenticate(String id, String password) {
if(!id.equals("madvirus")){
throw new AuthenticationException("invalid id"+ id);
}
}
}
PageRank *4 - 엑셀파일
package madvirus.spring.chap07.controller;
public class PageRank {
private int rank;
private String page;
public PageRank() {
}
public PageRank(int rank, String page) {
super();
this.rank = rank;
this.page = page;
}
public int getRank() {
return rank;
}
public String getPage() {
return page;
}
}
public class PageRank {
private int rank;
private String page;
public PageRank() {
}
public PageRank(int rank, String page) {
super();
this.rank = rank;
this.page = page;
}
public int getRank() {
return rank;
}
public String getPage() {
return page;
}
}
PageRanksController *4 - 엑셀파일 다운로드
package madvirus.spring.chap07.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PageRanksController {
@RequestMapping("/pageRanks")
public ModelAndView handleRequestInternal(){
List<PageRank> pageRanks = new ArrayList<PageRank>();
pageRanks.add(new PageRank(1, "/bbs/mir2/list"));
pageRanks.add(new PageRank(2, "/bbs/mir3/list"));
pageRanks.add(new PageRank(3, "/bbs/changchun2/list"));
return new ModelAndView("pageRanks","pageRanks",pageRanks);
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PageRanksController {
@RequestMapping("/pageRanks")
public ModelAndView handleRequestInternal(){
List<PageRank> pageRanks = new ArrayList<PageRank>();
pageRanks.add(new PageRank(1, "/bbs/mir2/list"));
pageRanks.add(new PageRank(2, "/bbs/mir3/list"));
pageRanks.add(new PageRank(3, "/bbs/changchun2/list"));
return new ModelAndView("pageRanks","pageRanks",pageRanks);
}
}
PageReportController *4 - pdf파일
package madvirus.spring.chap07.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PageReportController {
@RequestMapping("/pageReport")
public ModelAndView pdfReport(){
List<PageRank> pageRanks = new ArrayList<PageRank>();
pageRanks.add(new PageRank(1, "/bbs/mir2/list"));
pageRanks.add(new PageRank(2, "/bbs/mir3/list"));
pageRanks.add(new PageRank(3, "/bbs/changchun2/list"));
return new ModelAndView("pageReport","pageRanks",pageRanks);
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PageReportController {
@RequestMapping("/pageReport")
public ModelAndView pdfReport(){
List<PageRank> pageRanks = new ArrayList<PageRank>();
pageRanks.add(new PageRank(1, "/bbs/mir2/list"));
pageRanks.add(new PageRank(2, "/bbs/mir3/list"));
pageRanks.add(new PageRank(3, "/bbs/changchun2/list"));
return new ModelAndView("pageReport","pageRanks",pageRanks);
}
}
madvirus.spring.chap07.view
DownloadView *2
package madvirus.spring.chap07.view;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;
public class DownloadView extends AbstractView {
public DownloadView() {
setContentType("application/download; charset=utf-8");
}
@Override
protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
File file = (File) model.get("downloadFile");
response.setContentType(getContentType());
response.setContentLength((int) file.length());
String userAgent = request.getHeader("User-Agent");
boolean ie = userAgent.indexOf("MSIE") > -1;
String fileName = null;
if (ie) {
fileName = URLEncoder.encode(file.getName(), "utf-8");
} else {
fileName = new String(file.getName().getBytes("utf-8"),
"iso-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\";");
response.setHeader("Content-Transfer-Encoding", "binary");
OutputStream out = response.getOutputStream();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
FileCopyUtils.copy(fis, out);
} finally {
if (fis != null)
try {
fis.close();
} catch (IOException ex) {
}
}
out.flush();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;
public class DownloadView extends AbstractView {
public DownloadView() {
setContentType("application/download; charset=utf-8");
}
@Override
protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
File file = (File) model.get("downloadFile");
response.setContentType(getContentType());
response.setContentLength((int) file.length());
String userAgent = request.getHeader("User-Agent");
boolean ie = userAgent.indexOf("MSIE") > -1;
String fileName = null;
if (ie) {
fileName = URLEncoder.encode(file.getName(), "utf-8");
} else {
fileName = new String(file.getName().getBytes("utf-8"),
"iso-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\";");
response.setHeader("Content-Transfer-Encoding", "binary");
OutputStream out = response.getOutputStream();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
FileCopyUtils.copy(fis, out);
} finally {
if (fis != null)
try {
fis.close();
} catch (IOException ex) {
}
}
out.flush();
}
}
PageRanksView *4 - 엑셀파일
package madvirus.spring.chap07.view;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import madvirus.spring.chap07.controller.PageRank;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
public class PageRanksView extends AbstractExcelView {
//@SuppressWarnings("unchecked") : 문법에 문제가없는데 나오는 노란줄 경고 안나오게함
@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
HSSFSheet sheet = createFirstSheet(workbook);
createColumnLabel(sheet);
List<PageRank> pageRanks = (List<PageRank>) model.get("pageRanks");
int rowNum = 1;
for (PageRank rank : pageRanks) {
createPageRankRow(sheet, rank, rowNum++);
}
}
private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, "페이지 순위");
sheet.setColumnWidth(1, 256 * 20);
return sheet;
}
private void createColumnLabel(HSSFSheet sheet) {
HSSFRow firstRow = sheet.createRow(0);
HSSFCell cell = firstRow.createCell(0);
cell.setCellValue("순위");
cell = firstRow.createCell(1);
cell.setCellValue("페이지");
}
private void createPageRankRow(HSSFSheet sheet, PageRank rank, int rowNum) {
HSSFRow row = sheet.createRow(rowNum);
HSSFCell cell = row.createCell(0);
cell.setCellValue(rank.getRank());
cell = row.createCell(1);
cell.setCellValue(rank.getPage());
}
}
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import madvirus.spring.chap07.controller.PageRank;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
public class PageRanksView extends AbstractExcelView {
//@SuppressWarnings("unchecked") : 문법에 문제가없는데 나오는 노란줄 경고 안나오게함
@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
HSSFSheet sheet = createFirstSheet(workbook);
createColumnLabel(sheet);
List<PageRank> pageRanks = (List<PageRank>) model.get("pageRanks");
int rowNum = 1;
for (PageRank rank : pageRanks) {
createPageRankRow(sheet, rank, rowNum++);
}
}
private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, "페이지 순위");
sheet.setColumnWidth(1, 256 * 20);
return sheet;
}
private void createColumnLabel(HSSFSheet sheet) {
HSSFRow firstRow = sheet.createRow(0);
HSSFCell cell = firstRow.createCell(0);
cell.setCellValue("순위");
cell = firstRow.createCell(1);
cell.setCellValue("페이지");
}
private void createPageRankRow(HSSFSheet sheet, PageRank rank, int rowNum) {
HSSFRow row = sheet.createRow(rowNum);
HSSFCell cell = row.createCell(0);
cell.setCellValue(rank.getRank());
cell = row.createCell(1);
cell.setCellValue(rank.getPage());
}
}
PageReportView *4 - pdf파일
package madvirus.spring.chap07.view;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import madvirus.spring.chap07.controller.PageRank;
import org.springframework.web.servlet.view.document.AbstractPdfView;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class PageReportView extends AbstractPdfView {
@SuppressWarnings("unchecked")
@Override
protected void buildPdfDocument(Map<String, Object> model,
Document document, PdfWriter writer, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<PageRank> pageRanks = (List<PageRank>) model.get("pageRanks");
Table table = new Table(2, pageRanks.size() + 1);
table.setPadding(5);
BaseFont bfKorean = BaseFont.createFont(
"c:\\windows\\fonts\\batang.ttc,0", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
Font font = new Font(bfKorean);
Cell cell = new Cell(new Paragraph("순위", font));
cell.setHeader(true);
table.addCell(cell);
cell = new Cell(new Paragraph("페이지", font));
table.addCell(cell);
table.endHeaders();
for (PageRank rank : pageRanks) {
table.addCell(Integer.toString(rank.getRank()));
table.addCell(rank.getPage());
}
document.add(table);
}
}
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import madvirus.spring.chap07.controller.PageRank;
import org.springframework.web.servlet.view.document.AbstractPdfView;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class PageReportView extends AbstractPdfView {
@SuppressWarnings("unchecked")
@Override
protected void buildPdfDocument(Map<String, Object> model,
Document document, PdfWriter writer, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<PageRank> pageRanks = (List<PageRank>) model.get("pageRanks");
Table table = new Table(2, pageRanks.size() + 1);
table.setPadding(5);
BaseFont bfKorean = BaseFont.createFont(
"c:\\windows\\fonts\\batang.ttc,0", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
Font font = new Font(bfKorean);
Cell cell = new Cell(new Paragraph("순위", font));
cell.setHeader(true);
table.addCell(cell);
cell = new Cell(new Paragraph("페이지", font));
table.addCell(cell);
table.endHeaders();
for (PageRank rank : pageRanks) {
table.addCell(Integer.toString(rank.getRank()));
table.addCell(rank.getPage());
}
document.add(table);
}
}
messages
label_en.properties *1
login.form.title=Login Form
login.form.type=Login Type
login.form.id=ID
login.form.password=Password
login.form.submit=Login
login.form.type=Login Type
login.form.id=ID
login.form.password=Password
login.form.submit=Login
label.properties *1
login.form.title=로그인
login.form.type=로그인 타입
login.form.id=회원Id
login.form.password=비밀번호
login.form.submit=전송
login.form.type=로그인 타입
login.form.id=회원Id
login.form.password=비밀번호
login.form.submit=전송
validation_en.properties *1
required=required
required.login.id=login id is required
required.login.password=login password is requreid
required.login.loginType=You have to select login type
invalidIdOrPassword.login=Login id and password do not match. (You provided {0})
required.login.id=login id is required
required.login.password=login password is requreid
required.login.loginType=You have to select login type
invalidIdOrPassword.login=Login id and password do not match. (You provided {0})
validation.properties *1
required=필수입니다.
required.login.id=ID는 필수입니다.
required.login.password=비밀번호는 필수입니다.
required.login.loginType=로그인 타입을 선택하세요.
invalidIdOrPassword.login=당신이 입력한 id {0}. 로그인 아이디와 비밀번호가 일치하지 않습니다.
required.login.id=ID는 필수입니다.
required.login.password=비밀번호는 필수입니다.
required.login.loginType=로그인 타입을 선택하세요.
invalidIdOrPassword.login=당신이 입력한 id {0}. 로그인 아이디와 비밀번호가 일치하지 않습니다.
WebContent
viewjsp
loginForm.jsp *1
<%@ page contentType="text/html; charset=EUC-KR" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title><spring:message code="login.form.title"/></title>
</head>
<body>
<form:form commandName="login">
<form:errors />
<p>
<label for="loginType"><spring:message code="login.form.type" /></label>
<form:select path="loginType" items="${loginTypes}" />
</p>
<p>
<label for="id"><spring:message code="login.form.id" /></label>
<form:input id="id" path="id"/>
<form:errors path="id" />
</p>
<p>
<label for="password"><spring:message code="login.form.password" /></label>
<form:password id="password" path="password"/>
<form:errors path="password" />
</p>
<p>
<input type="submit" value="<spring:message code="login.form.submit" />">
</p>
</form:form>
</body>
</html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title><spring:message code="login.form.title"/></title>
</head>
<body>
<form:form commandName="login">
<form:errors />
<p>
<label for="loginType"><spring:message code="login.form.type" /></label>
<form:select path="loginType" items="${loginTypes}" />
</p>
<p>
<label for="id"><spring:message code="login.form.id" /></label>
<form:input id="id" path="id"/>
<form:errors path="id" />
</p>
<p>
<label for="password"><spring:message code="login.form.password" /></label>
<form:password id="password" path="password"/>
<form:errors path="password" />
</p>
<p>
<input type="submit" value="<spring:message code="login.form.submit" />">
</p>
</form:form>
</body>
</html>
loginSuccess.jsp *1
<%@ page language="java" contentType="text/html; charset=EUC-KR" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>로그인 성공</title>
</head>
<body>
로그인에 성공했습니다.
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>로그인 성공</title>
</head>
<body>
로그인에 성공했습니다.
</body>
</html>
dispatcherInternal-servlet.xml *1
<?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">
<!-- 커스텀태그이용 -->
<bean class="madvirus.spring.chap07.controller.LoginController">
<property name="authenticator">
<bean class="madvirus.spring.chap07.controller.MockAuthenticator"/>
</property>
</bean>
<!-- 국제화 -->
<bean class="madvirus.spring.chap07.controller.LocaleChangeController">
<property name="localeResolver" ref="localeResolver"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<!-- 국제화2 -->
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
</bean>
<!-- View 글로벌 설정 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/viewjsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 리소스 번들 지정 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages.validation</value>
<value>messages.label</value>
</list>
</property>
</bean>
</beans>
<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">
<!-- 커스텀태그이용 -->
<bean class="madvirus.spring.chap07.controller.LoginController">
<property name="authenticator">
<bean class="madvirus.spring.chap07.controller.MockAuthenticator"/>
</property>
</bean>
<!-- 국제화 -->
<bean class="madvirus.spring.chap07.controller.LocaleChangeController">
<property name="localeResolver" ref="localeResolver"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<!-- 국제화2 -->
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
</bean>
<!-- View 글로벌 설정 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/viewjsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 리소스 번들 지정 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages.validation</value>
<value>messages.label</value>
</list>
</property>
</bean>
</beans>
dispatcherNonHtml-servlet.xml *2
<?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-3.0.xsd">
<!-- 파일 다운로드 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean id="downloadController" class="madvirus.spring.chap07.controller.DownloadController"/>
<bean id="download" class="madvirus.spring.chap07.view.DownloadView"></bean>
<!-- 엑셀 다운로드 -->
<bean id="pageRanksController" class="madvirus.spring.chap07.controller.PageRanksController" />
<bean id="pageRanks" class="madvirus.spring.chap07.view.PageRanksView" />
<!-- PDF 다운로드 -->
<bean id="pageReportController" class="madvirus.spring.chap07.controller.PageReportController" />
<bean id="pageReport" class="madvirus.spring.chap07.view.PageReportView"/>
</beans>
<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-3.0.xsd">
<!-- 파일 다운로드 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean id="downloadController" class="madvirus.spring.chap07.controller.DownloadController"/>
<bean id="download" class="madvirus.spring.chap07.view.DownloadView"></bean>
<!-- 엑셀 다운로드 -->
<bean id="pageRanksController" class="madvirus.spring.chap07.controller.PageRanksController" />
<bean id="pageRanks" class="madvirus.spring.chap07.view.PageRanksView" />
<!-- PDF 다운로드 -->
<bean id="pageReportController" class="madvirus.spring.chap07.controller.PageReportController" />
<bean id="pageReport" class="madvirus.spring.chap07.view.PageReportView"/>
</beans>
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">
<context-param>
<param-name>defaultHtmlEscape</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherInternal</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherInternal</servlet-name>
<url-pattern>/jsp/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcherNonHtml</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherNonHtml</servlet-name>
<url-pattern>/download/*</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>
<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">
<context-param>
<param-name>defaultHtmlEscape</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherInternal</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherInternal</servlet-name>
<url-pattern>/jsp/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcherNonHtml</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherNonHtml</servlet-name>
<url-pattern>/download/*</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>
설명.txt
index.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>View 연습</title>
</head>
<body>
<a href="http://localhost:8080/chap07/jsp/login/login.do">LoginController </a><br>
<a href="http://localhost:8080/chap07/download/file">DownloadController - 파일 다운로드</a><br>
<a href="http://localhost:8080/chap07/jsp/changLanguage?lang=ko">LocaleChangeController -1 한글</a><br>
<a href="http://localhost:8080/chap07/jsp/changLanguage?lang=en">LocaleChangeController -2 영어</a><br>
<a href="http://localhost:8080/chap07/jsp/login/login.do?language=ko">LocaleChangeInterceptor -1 한글</a><br>
<a href="http://localhost:8080/chap07/jsp/login/login.do?language=en">LocaleChangeInterceptor -2 영어</a><br>
<a href="http://localhost:8080/chap07/download/pageRanks">PageRanksController - 엑셀파일 다운로드</a><br>
<a href="http://localhost:8080/chap07/download/pageReport">PageReportController - PDF파일 다운로드</a><br>
</body>
</html>
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>View 연습</title>
</head>
<body>
<a href="http://localhost:8080/chap07/jsp/login/login.do">LoginController </a><br>
<a href="http://localhost:8080/chap07/download/file">DownloadController - 파일 다운로드</a><br>
<a href="http://localhost:8080/chap07/jsp/changLanguage?lang=ko">LocaleChangeController -1 한글</a><br>
<a href="http://localhost:8080/chap07/jsp/changLanguage?lang=en">LocaleChangeController -2 영어</a><br>
<a href="http://localhost:8080/chap07/jsp/login/login.do?language=ko">LocaleChangeInterceptor -1 한글</a><br>
<a href="http://localhost:8080/chap07/jsp/login/login.do?language=en">LocaleChangeInterceptor -2 영어</a><br>
<a href="http://localhost:8080/chap07/download/pageRanks">PageRanksController - 엑셀파일 다운로드</a><br>
<a href="http://localhost:8080/chap07/download/pageReport">PageReportController - PDF파일 다운로드</a><br>
</body>
</html>
===============================결과값====================================
1.로그인 테스트
en_US
ko_KR
2.다운로드
3. 국제화 LocaleReslover를 이용한 Locale 변경
국제화 LocaleChangeInterceptor
4.엑셀파일 다운로드 , PDF 다운로드
'스프링3.0' 카테고리의 다른 글
Spring3.0 Tiles (1) | 2012.05.14 |
---|---|
Spring3.0 DB (0) | 2012.05.14 |
Spring3.0 MVC 웹요청 처리 (0) | 2012.05.14 |
Spring 3.0 AOP (0) | 2012.05.14 |
Spring3.0 DI 3 (0) | 2012.05.14 |