maven+springMVC(二)

 【目录】

  😝新建maven项目

  😁搭建springMVC

  😎使用MyBatis连接数据库

  😊MyBatis整合SpringMVC

  😂MyBatis逆向工程

 

【步骤】

①修改pop.xml(<dependencies>标签)

 1       <dependency>  
 2         <groupId>javax.servlet</groupId>  
 3         <artifactId>servlet-api</artifactId>  
 4         <version>2.5</version>  
 5         <scope>provided</scope>  
 6       </dependency>
 7 
 8       <dependency>
 9           <groupId>org.springframework</groupId>
10           <artifactId>spring-web</artifactId>
11           <version>4.1.2.RELEASE</version>
12            <scope>compile</scope>
13       </dependency>
14       <dependency>
15           <groupId>org.springframework</groupId>
16           <artifactId>spring-webmvc</artifactId>
17           <version>4.1.2.RELEASE</version>
18            <scope>compile</scope>
19       </dependency>

 

②修改web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 5 
 6     <servlet>
 7         <servlet-name>spring</servlet-name> <!-- 属性随意,只要上下一致即可 -->
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9     </servlet>
10 
11     <servlet-mapping>
12         <servlet-name>spring</servlet-name>  <!-- 与上方一致 -->
13         <url-pattern>/</url-pattern><!-- 拦截所有请求 -->
14     </servlet-mapping>
15 
16 </web-app>

 

🌂以上配置会直接在WEB-INF中查找对应  “服务名-servlet.xml”,此处即,spring-servlet.xml(没有则创建)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6        http://www.springframework.org/schema/beans/spring-beans.xsd 
 7        http://www.springframework.org/schema/context 
 8        http://www.springframework.org/schema/context/spring-context.xsd 
 9        http://www.springframework.org/schema/tx 
10        http://www.springframework.org/schema/tx/spring-tx.xsd
11           http://www.springframework.org/schema/mvc
12        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
13 
14     <!-- 配置扫描的包(此配置会自动扫描com.springdemo下的所有包中的含有注解的类(如@Controller, @Service等)) -->
15     <context:component-scan base-package="com.springdemo.*" />
16 
17     <!-- 注册HandlerMapper、HandlerAdapter两个映射类 ,负责将请求映射到类和方法中-->
18     <mvc:annotation-driven />
19 
20     <!-- 访问静态资源 ,因为配置的spring是拦截所有请求,需如下配置来让静态资源通过(如js, css文件等) -->
21     <mvc:default-servlet-handler />
22     
23     <!-- 视图解析器 :是将Controller类返回的视图名加上配置的前后缀进行展示-->
24     <bean
25         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26         <property name="prefix" value="/WEB-INF/view/"></property>
27         <property name="suffix" value=".jsp"></property>
28     </bean>
29     
30 </beans>

 

④创建视图(在WEB-INF下创建view文件夹,view文件夹下创建demo.jsp)

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="utf-8">
 7 <title>DomePage</title>
 8 </head>
 9 <body>
10 
11 <h1>This is springMVC Demo Page</h1>
12 </body>
13 </html>

 

😭创建controller(创建com.springdemo.controller包,包下创建DemoController.java文件)

  

 1 package com.springdemo.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 @RequestMapping("/demo")
 8 public class DemoController {
 9 
10     @RequestMapping("/index")
11     public String index(){
12         return "demo";
13     }
14 }

 

⑥Tomcat部署访问地址(http://localhost:8080/demo/demo/index

  

 

posted @ 2019-02-13 22:23  茄子鱼  阅读(2411)  评论(0编辑  收藏  举报