Java springmvc集成

Java springmvc集成,java,maven,spring-mvc,controller,thymeleaf,Java,Maven,Spring Mvc,Controller,Thymeleaf,我有个问题。我创建了SpringMVC+Thymeleft项目,但无法正确集成,我阅读了许多指南和文档,但没有任何帮助。有什么问题吗?页面加载,无错误,但无相关信息。。我的代码: spring.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSc

我有个问题。我创建了SpringMVC+Thymeleft项目,但无法正确集成,我阅读了许多指南和文档,但没有任何帮助。有什么问题吗?页面加载,无错误,但无相关信息。。我的代码:

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>

SpringMVC1.model.Store
org.hibernate.dialogue.mysqldialogue
线
假的
假的
home.jsp
在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>

org.thymeleaf
百里香
2.1.0.1发布
org.thymeleaf
百里香
2.1.1.1发布
控制器原型

@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");
}
*/
}
@控制器
公共类家庭控制器{
@自动连线
私有ServletContext ServletContext;
模板引擎模板引擎;
私人静态存储;
私有静态StoreDAOImpl StoreDAOImpl;
静态列表存储;
静止的{
ClassPathXmlApplicationContext上下文=新的ClassPathXmlApplicationContext(“spring.xml”);
storeDAOImpl=(storeDAOImpl)context.getBean(“storeDAO”);
store=(store)context.getBean(“storeModel”);
}
/*
@请求映射(value=“/home”)
公共模型和视图句柄请求(HttpServletRequest请求,
HttpServletResponse(响应)引发异常{
ModelAndView ModelAndView=新的ModelAndView(“主页”);
//modelAndView.addObject(“storeList”,storeDAO.getAllStores());
返回模型和视图;
}
@请求映射(value=“/storeModel”)
公共模型和视图根(@PathVariable String appBeanId,Locale,
org.springframework.ui.Model){
appBeanId=“storeModel”;
locale.getCountry();
store.setName(“store1”);
model.addAttribute(“store1”,store);
返回新模型和视图(“主页”);
}
*/
私有静态最终记录器Logger=LoggerFactory.getLogger(HomeController.class);
@请求映射(value=“/home”)
公共字符串登录(@validatedstore,Model){
model.addAttribute(“storeName”、“sometext”);
返回“家”;
}
/*
//public static List stores=storeDAOImpl.getAllStores();
@请求映射(value=“/listStores”)
公共模型和视图y(HttpServletRequest请求,
HttpServletResponse resp)引发SQLException、IOException{
store.setName(“store1”);
WebContext WebContext=新的WebContext(请求、响应、servletContext);
setVariable(“stores”,storeDAOImpl.getAllStores());
setVariable(“store1”,store);
返回新模型和视图(“主页”);
}
*/
}
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>

家
你好,世界!
这是商店

商店#2 0

据我所知,从控制器返回jsp页面时,u应使用smth,如:

返回新的ModelAndView(“home.jsp”)

这里是关于返回jsp的

如果字符串像u一样返回,则使用它返回存储在类路径中的html页面。在thymeleaf的上下文中,它通常被命名为模板

为什么u试图同时使用thymeleaf和jsp。我可能错了,但这是两种相互竞争的技术


在官方的thymeleaf上:“thymeleaf提供了一组Spring集成,允许您在Spring MVC应用程序中使用它作为JSP的全功能替代品。”

我在一个公共github中发布了一个工作(使用jetty测试)解决方案

控制器具有以下代码:@RequestMapping(value=“/home”)公共模型和视图HandlerRequest(HttpServletRequest-req,HttpServletResponse-resp)引发异常{ModelAndView ModelAndView=new ModelAndView(“home”);ModelAndView.addObject(“store”,“Sometext”);return ModelAndView;}我在home.jsp中写道:0,但它不工作..尝试
newmodelandview(“home”)更改为
新模型和视图(“home.jsp”)并确保home.jsp位于类路径中。在jsp中使用“th”名称空间是为了什么?不是jstl for jsp,也不是带有thymeleaf而不是jsp文件的html模板。如何确保类路径中的home.jsp?)我添加了以下内容:在templateResolver中,这意味着我只能返回home,没有后缀)