2014-12-25 4 views
1

문제가 있습니다. Spring MVC + Thymeleft 프로젝트를 만들었지 만 제대로 통합 할 수 없기 때문에 많은 가이드와 문서를 읽었지만 나에게 도움이되지 못했습니다. 무슨 일 이니? 페이지로드 및 오류없이 있지만 관련 정보 .. 내 코드 :스프링 MVC 통합 Thymeleaf

spring.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" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/practick" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 
</bean> 

<!-- Hibernate 3 Annotation SessionFactory Bean definition--> 
<bean id="hibernate3AnnotatedSessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <value>SpringMVC1.model.Store</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.current_session_context_class">thread</prop> 
      <prop key="hibernate.enable_lazy_load_no_trans">false</prop> 
      <prop key="hibernate.show_sql">false</prop> 
     </props> 
    </property> 
</bean> 

<mvc:resources mapping="/SpringMVC1/**" location="/WEB-INF/views" /> 

<!-- class beans --> 
<bean id="storeDAO" class="SpringMVC1.dao.Impl.StoreDAOImpl"> 
    <property name="sessionFactory" ref="hibernate3AnnotatedSessionFactory" /> 
</bean> 

<bean id="storeModel" class="SpringMVC1.model.Store"> 
</bean> 

<!-- Thymeleaf --> 
<bean id="templateResolver" 
     class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> 
    <property name="prefix" value="/views/" /> 
    <property name="suffix" value=".jsp" /> 
    <property name="templateMode" value="HTML5" /> 
</bean> 

<bean id="templateEngine" 
     class="org.thymeleaf.spring3.SpringTemplateEngine"> 
    <property name="templateResolver" ref="templateResolver" /> 
    <property name="additionalDialects"> 
     <set> 
      <bean class = "org.thymeleaf.spring3.dialect.SpringStandardDialect"/> 
     </set> 
    </property> 
</bean> 

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver"> 
    <property name="templateEngine" ref="templateEngine" /> 
    <property name="order" value="1" /> 
    <property name="viewNames" value="*.html,*.xhtml,*.jsp" /> 
    <property name="excludedViewNames"> 
     <array> 
      <value>home.jsp</value> 
     </array> 
    </property> 
</bean> 

</beans> 

삽입 의존성의 pom.xml 에서 : 컨트롤러의

<!-- Thymeleaf --> 
    <dependency> 
     <groupId>org.thymeleaf</groupId> 
     <artifactId>thymeleaf</artifactId> 
     <version>2.1.0.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.thymeleaf</groupId> 
     <artifactId>thymeleaf-spring3</artifactId> 
     <version>2.1.1.RELEASE</version> 
    </dependency> 

프로토 타입

@Controller 
public class HomeController { 

@Autowired 
private ServletContext servletContext; 

TemplateEngine templateEngine; 

private static Store store; 
private static StoreDAOImpl storeDAOImpl; 
static List<Store> storess; 

static{ 
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
    storeDAOImpl = (StoreDAOImpl)context.getBean("storeDAO"); 
    store = (Store)context.getBean("storeModel"); 
} 

/* 
@RequestMapping(value="/home") 
public ModelAndView handleRequest(HttpServletRequest req, 
            HttpServletResponse resp) throws Exception { 

    ModelAndView modelAndView = new ModelAndView("home"); 
    //modelAndView.addObject("storeList", storeDAO.getAllStores()); 

    return modelAndView; 
} 

    @RequestMapping(value = "/storeModel") 
    public ModelAndView root(@PathVariable String appBeanId, Locale locale, 
         org.springframework.ui.Model model) { 
     appBeanId="storeModel"; 
     locale.getCountry(); 
     store.setName("store1"); 
     model.addAttribute("store1", store); 
     return new ModelAndView("home"); 
    } 
*/ 
private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

@RequestMapping(value = "/home") 
public String login(@Validated Store store, Model model) { 
    model.addAttribute("storeName", "sometext"); 
    return "home"; 
} 


/* 
//public static List stores = storeDAOImpl.getAllStores(); 

@RequestMapping(value = "/listStores") 
public ModelAndView y(HttpServletRequest req, 
         HttpServletResponse resp) throws SQLException, IOException { 
    store.setName("store1"); 
    WebContext webContext = new WebContext(req, resp, servletContext); 
    webContext.setVariable("stores", storeDAOImpl.getAllStores()); 
    webContext.setVariable("store1", store); 
    return new ModelAndView("home"); 
} 
*/ 
} 

그리고 home.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<!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>Home</title> 
</head> 
<body> 
    <h1>Hello World!</h1> 
    <p>This is the stores!</p> 
<!--  <c:forEach items="${storeList}" var="store"> 
     ${store.name} ${store.type}: ${store.address} 
     <br /> 
    </c:forEach> 
--> 
<table> 
    <caption> Store #2</caption> 
    <tr></tr> 
     <td><span th:text = "${storeName}">0</span></td> 
</table> 

</body> 
</html> 

web 내가 유처럼 떨어지게를 사용해야 컨트롤러에서 JSP 페이지를 반환하기위한 알고 project structure

답변

0

:

return new ModelAndView("home.jsp")

여기에 있었다 question 반송 jsp

String이 classpath에 저장된 html 페이지를 반환하는 데 사용되는 것처럼 반환되는 경우. thymeleaf의 맥락에서 보통 템플릿으로 명명되었습니다.

왜 thymeleaf와 jsp를 모두 사용하려고합니까? 나는 틀릴 수도 있지만 이것들은 두 가지 경쟁 기술입니다.

공식적인 thymeleaf site : "Thymeleaf는 Spring MVC 응용 프로그램에서 JSP 대신 사용할 수있는 일련의 Spring 통합을 제공합니다."

+0

컨트롤러는 다음 코드를 가지고 \t @RequestMapping (값 = "/ 홈") \t 공공의 ModelAndView handleRequest (HttpServletRequest의 REQ, \t \t \t \t을 \t \t \t \t \t HttpServletResponse를 RESP)는 예외 { \t \t의 ModelAndView를 슬로우 modelAndView = new ModelAndView ("home"); \t \t modelAndView.addObject ("store", "someteext"); \t \t return modelAndView; \t} home.jsp에서 다음과 같이 썼습니다 :하지만 작동하지 않습니다 .. – badCoder

+0

'새로운 ModelAndView ("home");'새로운 ModelAndView (' "home.jsp");'classpath에 home.jsp가 있는지 확인하십시오. 그리고 jsp에서 "th"네임 스페이스를 사용하는 이유는 무엇입니까? jsp는 jstl이 아니고, jsp 파일 대신 thymeleaf가있는 html 템플릿이 있습니다. – Cuzz

+0

클래스 패스에서 home.jsp를 확인하려면 어떻게해야합니까? – badCoder

0

공용 github project에 근무중인 (부두 테스트를 마친) 솔루션을 게시했습니다.

관련 문제