SpingMVC视图解析报错


问题描述

例如:SpringMVC 实践中配置好了web.xml以及applicationContext.xml 出现无法解析视图

无法解析 MVC 视图 'index'

服务器报错:

500
javax.servlet.ServletException: Servlet[SpringMVC]的Servlet.init()引发异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewResolver' defined in class path resource [springmvc.xml]: Cannot create inner bean 'org.thymeleaf.spring5.SpringTemplateEngine#563db04d' of type [org.thymeleaf.spring5.SpringTemplateEngine] while setting bean property 'templateEngine'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.thymeleaf.spring5.SpringTemplateEngine#563db04d' defined in class path resource [springmvc.xml]: Cannot create inner bean 'org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver#290264bf' of type [org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver] while setting bean property 'templateResolver'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver#290264bf' defined in class path resource [springmvc.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.thymeleaf.templatemode.TemplateMode' for property 'templateMode'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.thymeleaf.templatemode.TemplateMode' for property 'templateMode': no matching editors or conversion strategy found

原因分析以及解决方案:

在applicationContext中templateMode的value为HTML5,应该改为HTML

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个简单的 PHP MVC 框架实例: 1. 创建一个 index.php 文件,作为入口文件,内容如下: ``` <?php // 加载配置文件和自动加载类 require_once 'config.php'; require_once 'autoload.php'; // 实例化路由类,解析 URL $router = new Router(); $router->parseUrl(); // 根据解析后的控制器和方法,实例化控制器并调用方法 $controllerName = $router->getControllerName(); $methodName = $router->getMethodName(); $controller = new $controllerName(); $controller->$methodName(); ``` 2. 创建一个 config.php 文件,用来定义数据库连接等常量,内容如下: ``` <?php define('DB_HOST', 'localhost'); define('DB_NAME', 'test'); define('DB_USER', 'root'); define('DB_PASS', 'password'); ``` 3. 创建一个 autoload.php 文件,用来自动加载类文件,内容如下: ``` <?php spl_autoload_register(function ($classname) { $filename = strtolower($classname) . '.php'; if (file_exists($filename)) { require_once $filename; } }); ``` 4. 创建一个 Router 类,用来解析 URL,内容如下: ``` <?php class Router { private $controllerName; private $methodName; private $params = array(); public function parseUrl() { if (isset($_GET['url'])) { $url = $_GET['url']; $url = rtrim($url, '/'); $url = filter_var($url, FILTER_SANITIZE_URL); $url = explode('/', $url); $this->controllerName = ucfirst($url[0]) . 'Controller'; $this->methodName = isset($url[1]) ? $url[1] : 'index'; array_shift($url); array_shift($url); $this->params = $url; } else { $this->controllerName = 'HomeController'; $this->methodName = 'index'; } } public function getControllerName() { return $this->controllerName; } public function getMethodName() { return $this->methodName; } public function getParams() { return $this->params; } } ``` 5. 创建一个 BaseController 类,作为所有控制器的父类,内容如下: ``` <?php class BaseController { protected function loadModel($model) { require_once 'models/' . $model . '.php'; return new $model(); } protected function view($view, $data = array()) { require_once 'views/' . $view . '.php'; } protected function redirect($url) { header('Location: ' . $url); exit; } } ``` 6. 创建一个 HomeController 类,作为默认控制器,内容如下: ``` <?php class HomeController extends BaseController { public function index() { $this->view('home/index'); } } ``` 7. 创建一个 models 目录和一个 views 目录,用来存放模型和视图文件。 以上就是一个简单的 PHP MVC 框架实例。可以根据需要添加更多的控制器、模型和视图文件,以及实现更多的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值