- ViewResolver
- View객체에서 제일 중요한것은 render 메소드다.
- ViewResolver객체에서 제일 중요 한 것은 resolveViewName 메소드다.
- InternalResourceViewResolver는 내부적으로 JstlView 를 사용하고 forward 해준다.
- 사용자가 요청한 타입에 따라서 다른 결과를 보여주고 싶을때는 ContentNegotiationManager 를 사용한다.(pdf, json등)
@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager contentNegotiationManager) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(contentNegotiationManager);
// Define all possible view resolvers
List resolvers = new ArrayList();
resolvers.add(new HtmlViewResolver());
resolvers.add(new JsonViewResolver()); // http://localhost:8080/guestbooks.json
resolver.setViewResolvers(resolvers);
return resolver;
}
@Bean
public ContentNegotiationManager contentNegotiationManager(){
ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
contentNegotiationManager.setFavorPathExtension(true);
contentNegotiationManager.setIgnoreAcceptHeader(true);
contentNegotiationManager.setUseJaf(false);
contentNegotiationManager.setDefaultContentType(MediaType.TEXT_HTML);
contentNegotiationManager.addMediaType("html", MediaType.TEXT_HTML);
contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);
return contentNegotiationManager.getObject();
}
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import java.util.Locale;
public class JsonViewResolver implements ViewResolver {
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return new JsonView();
}
}
import org.springframework.web.servlet.View;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;
public class JsonView implements View{
@Override
public String getContentType() {
return "application/json";
}
@Override
public void render(Map map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletResponse.setContentType("application/json");
PrintWriter out = httpServletResponse.getWriter();
out.println("{\"name\":\"urstory\"}");
}
}
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import java.util.Locale;
public class HtmlViewResolver implements ViewResolver {
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return new HtmlView();
}
}
import org.springframework.web.servlet.View;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;
public class HtmlView implements View{
@Override
public String getContentType() {
return "text/html";
}
@Override
public void render(Map map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletResponse.setContentType("text/html");
PrintWriter out = httpServletResponse.getWriter();
out.println("hello!!!");
}
}
0 개의 댓글:
댓글 쓰기