码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc的搭建

时间:2020-04-30 11:17:17      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:依赖   value   控制器   jsp   font   cat   size   web   request   

引入依赖包

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>

</dependency>


web.xml
<!-- springmvc -->
<servlet>
<!-- 注册DispatcherServlet -->
<servlet-name>dispatcherservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- SpringMvc.xml文件的路径 -->
<param-value>classpath:/configuration/SpringMvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- 匹配controller的规则 -->
<servlet-name>dispatcherservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


springmvc.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:context="http://www.springframework.org/schema/context"
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-4.0.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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
">

<!-- 开启注解扫描 -->
<context:component-scan base-package="com.gxy.controller"></context:component-scan>

<!-- mvc注解驱动,自动注册MVC依赖的内置bean -->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 视图解析器 (把逻辑视图解析成物理视图)-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>


</beans>


Controller.java


//@Controller(功能和@Component一样;但他不仅注册了Bean并且将此类设置成了控制器);
@Controller
public class HelloHandler {
//映射请求地址
@RequestMapping("/index")
public String hello(){
System.out.println("index......");
//return一个页面名字,由视图解析器解析成物理路径;
return "index";
}
}

 

springmvc的搭建

标签:依赖   value   控制器   jsp   font   cat   size   web   request   

原文地址:https://www.cnblogs.com/loveweni/p/12807555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!