【Spring】—Application Case—Annotation-based Spring MVC Application

Application case - annotation-based Spring MVC application

In order to help readers get familiar with the use of the core classes and annotations of Spring MVC, the introductory case will be rewritten in the form of annotations. The specific implementation steps are as follows.

1. Build the project environment

Create a Web project named chapter11_2, copy all the JAR packages in the chapter11 project and all the files written to the chapter11_2 project, and add the JARs required by Spring AOP to the lib directory.

2. Modify the configuration file

Add annotation scanning configuration in springmvc-config.xml and define view resolver.

<?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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        <!--  指定需要扫描的包  -->
        <context:component-scan base-package="com.ssm.controller"/>
    <!--  定义视图解析器  -->
    <bean id="viewResoler"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <!--  设置前缀  -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!--  设置后缀  -->
    <property name="suffix" value=".jsp" />
    </bean>
</beans>

First, the package to be scanned is specified through the component scanner, and then the view resolver is defined, and the path prefix and file extension of the view file are set in the view resolver.

3. Modify the Controller class

Modify the ControllerTest class and add corresponding annotations to the class and method.

package com.ssm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

//Controller注解
@Controller
//@RequestMapping注解
@RequestMapping(value = "/controll")
public class ControllerTest {
    
    
    @RequestMapping(value="/annotationController")
    public String handleRdequest(HttpServletRequest arg0, HttpServletResponse arg1,
                                Model model) throws Exception{
    
    
        model.addAttribute("msg","第一个Spring MVC程序");
        return "welcome";
   }
}

The @Controller annotation is used to annotate the controller class, and the @RequestMapping annotation is used to annotate the class name and method name to map the request method. When the project starts, Spring will scan this class and the methods annotated with @RequestMapping annotation in this class. Since the value of the @RequestMapping annotation marked on the class is "/control", the path of all request methods in the class needs to add "/control". Since the return type of the handleRequest() method in the class is String, and the return value of the String type cannot carry data, it is necessary to add data information through the addAttribute() method of the parameter Model object. Because the prefix and suffix of the view file are defined in the view parser of the configuration file, the handleRequest() method only needs to return the view name "welcome". When accessing this method, the system will automatically access the "WEB(-? )INF/jsp/" path to the jsp file named welcome.

4. Start the project and test the application

Publish the project to the Tomcat server and start it, and visit the address http://localhost:8080/chapter11_2/control/annotationController in the browser.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45627039/article/details/131261425