您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
08-SpringMVC简介-SpringMVC快速入门代码实现(并解决Could not open ServletContext resource [jdbc.properties]问题)
发布时间:2024-12-26 20:40:21编辑:雪饮阅读()
-
基于上篇
http://www.gaojiupan.cn/manshenghuo/chengxurensheng/5837.html
从web.xml中获取Spring Config配置文件的文件名的spring官方正统实现方式
接下来我们继续基于Spring-mvc实现更简单的servlet被访问并跳转到jsp的实现。
在pom.xml中新增依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
load-on-startup值为1表示每次启动服务启动就创建一次servlet,如果不配置则默认是被访问时候才创建,url-pattern配置为/,表示任何从根访问下去的路径都会先经过该servlet
并且Spring mvc也有类似Spring Config那样的xml配置文件,所以我这里需要配置一个servlet于web.xml中如:
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
那么基于此我们就来新增Spring-mvc.xml于resources目录中
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.web.controller"/>
</beans>
这里需要注意的是context标签默认不支持,需要额外引用的
xmlns:context=http://www.springframework.org/schema/context
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
"
那么可以看到这里是扫描一个我们假定的com.web.controller这个package,那么所以建立该package并建立一个遵循spring-mvc的controller如:
package com.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
//浏览器访问到本方法的url-patterns
@RequestMapping("/quick")
public String save(){
System.out.println("UserController save running");
return "success.jsp";
}
}
那么这里可以进一步推导我们需要一个success.jsp于webapp根目录中如:
<%--
Created by IntelliJ IDEA.
User: 1
Date: 2024/12/26
Time: 20:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
success
</body>
</html>
不过有一点是必须注意一下的,就是之前在JDBCApplicationContext.xml中有配置如
<context:property-placeholder location="jdbc.properties"/>
这样的配置在Spring-mvc之后好像会导致无法读取到jdbc.properties
然后修改后如:
<context:property-placeholder location="classpath:jdbc.properties"/>
就没有问题了。
否则就会报错如:
Could not open ServletContext resource [/jdbc.properties]
那最后服务起来后你可以访问如:
http://localhost:8080/untitled-1.0-SNAPSHOT/quick
查看效果。
关键字词:SpringMVC,spring,mvc
上一篇:05-Spring集成web环境-Spring集成web环境代码实现(从web.xml中获取Spring Config配置文件的文件名的spring官方正统实现方式)
下一篇:11-SpringMVC组件解析-SpringMVC注解解(限定http请求参数名及get、post请求方式)析
相关文章
- 05-Spring集成web环境-Spring集成web环境代码实现(从w
- 04-Spring集成web环境-自定义ContextLoaderListener2(
- 03-Spring集成web环境-自定义ContextLoaderListener1(
- 01-Spring集成web环境-基本三层架构环境搭建(浏览器访
- 16-Spring集成Junit-代码实现(优化测试需要获取Spring
- 14-Spring注解开发-新注解详解(全注解类代替Spring Co
- 10-Spring注解开发-原始注解入门操作(SpringConfig配
- 09-Spring注解开发-完善测试环境
- 07-Spring配置数据源-Spring(配置文件中)加载properti
- 06-Spring配置数据源-Spring产生数据源对象(SpringCon