springMVC注解方式

一、web.xml
  • 配置tomcat启动时加载DispatcherServlet
  • 配置spring配置文件的路径和名称
<web-app version="4.0"
      xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
      <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 修改配置文件路径和名称 -->
            <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <!-- 自启动,tomcat启动时加载,否则第一次访问时加载 -->
            <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
      </servlet-mapping>
  <display-name>Archetype Created Web Application</display-name>
</web-app>
 
 
 
二、springmvc.xml
  • 配置自动扫描注解
  • 配置自动加载mvc注解
  • 配置解决中文乱码问题
<?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:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
    <!-- 扫描注解 -->
      <context:component-scan  base-package="top.woldcn"></context:component-scan>   
      
    <!-- 加载mvc注解 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 解决Controller返回中文乱码问题 -->
            <bean  class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>   
</beans>
 
三、DemoController.java
  • @Controller:配置该类为controller
  • @GetMapping("/test"):配置url映射
  • @ResponseBody:配置方法返回值为返回给前端
  • @Autowired:配置自动装载的service
package top.woldcn.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.woldcn.service.DemoService;
@Controller
public class DemoController {
       @Autowired
      private DemoService demoService;
      
      @GetMapping("/test")
      @ResponseBody
      public String demo() {
            return demoService.demo();
      }
}
 
四、DemoService.java
  • @Service:配置该类为service
package top.woldcn.service;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
      public String demo(){
            return "this is a test测试";
      }
}
 

猜你喜欢

转载自www.cnblogs.com/ruowei/p/10852171.html