这是我的项目
\n\n\n\n这是我的 spring-mvc.xml
\n\n<?xml version="1.0" encoding="UTF-8"?>\n<beans xmlns="http://www.springframework.org/schema/beans"\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xmlns:context="http://www.springframework.org/schema/context"\n xmlns:mvc="http://www.springframework.org/schema/mvc"\n xsi:schemaLocation="\n http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd\n http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd\n http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">\n <context:component-scan base-package="com.SpringDemo.Controller"/>\n <!-- Thymeleaf Template Resolver -->\n <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">\n <property name="prefix" value="" />\n <property name="suffix" value=".html" />\n <property name="templateMode" value="HTML" />\n <property name="order" value="1" />\n </bean>\n\n <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">\n <property name="templateResolver" ref="templateResolver" />\n <property name="enableSpringELCompiler" value="true" />\n </bean>\n\n <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">\n <property name="viewNames" value="thymeleaf/*"/>\n <property name="templateEngine" ref="templateEngine" />\n </bean>\n\n <!-- \xe5\xaf\xb9\xe6\xa8\xa1\xe5\x9e\x8b\xe8\xa7\x86\xe5\x9b\xbe\xe5\x90\x8d\xe7\xa7\xb0\xe7\x9a\x84\xe8\xa7\xa3\xe6\x9e\x90\xef\xbc\x8c\xe5\x8d\xb3\xe5\x9c\xa8\xe6\xa8\xa1\xe5\x9e\x8b\xe8\xa7\x86\xe5\x9b\xbe\xe5\x90\x8d\xe7\xa7\xb0\xe6\xb7\xbb\xe5\x8a\xa0\xe5\x89\x8d\xe5\x90\x8e\xe7\xbc\x80 -->\n <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">\n <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />\n\n <property name="prefix" value="" />\n <!--<property name="suffix" value=".jsp" />-->\n <property name="order" value="2" />\n <property name="viewNames" value="*.jsp" />\n </bean>\n<!-- \xe6\x94\xaf\xe6\x8c\x81\xe4\xb8\x8a\xe4\xbc\xa0\xe6\x96\x87\xe4\xbb\xb6 -->\n <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>\n</beans>\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的控制器
\n\n@Controller\npublic class HomeController{\n //It\'s OK\n @RequestMapping("/home")\n public String showHomePage(Model model){\n model.addAttribute("name","spring-mvc");\n return "thymeleaf/testTh";\n }\n //It\'s WRONG\n @RequestMapping("/index")\n public ModelAndView test(ModelAndView mv){\n mv.addObject("name","erer");\n mv.setViewName("index.jsp");\n return mv;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的例外
\n\n\n\n在我的 Web.xml 中
\n\n <servlet>\n <servlet-name>springmvc</servlet-name>\n <servlet-class>org.springframework.web.servlet.DispatcherServlet\n </servlet-class>\n <!-- \xe5\x8a\xa0\xe8\xbd\xbdspringMVC\xe7\x9a\x84\xe9\x85\x8d\xe7\xbd\xae\xe6\x96\x87\xe4\xbb\xb6 -->\n <init-param>\n <param-name>contextConfigLocation</param-name>\n <param-value>classpath:spring-mvc.xml</param-value>\n </init-param>\n <load-on-startup>1</load-on-startup>\n</servlet>\n\n<servlet-mapping>\n <servlet-name>springmvc</servlet-name>\n <url-pattern>/*</url-pattern>\n</servlet-mapping>\nRun Code Online (Sandbox Code Playgroud)\n\n我希望所有请求都首先通过控制器传递。\n然后返回页面
\n小智 5
请导入 spring-boot-starter-thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6784 次 |
| 最近记录: |