鼓捣了一上午的springmvc配置

因为临时要用所以决定先搭个SSM整合包再挨个深究

spring的昨天就已经做好了,只不过因为Tomcat的原因大概会在这之后写笔记吧

首先,是路径配置以及用到的文件
文件路径

配置步骤

1.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="4.0">

    <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-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

2.新建配置文件springmvc-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--处理映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
    <!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    <!--视图解析器-->    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>

3.建一个Controller类

package top.moesy.controller;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

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

public class ControllerTest1 implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    
        ModelAndView mv = new ModelAndView();
        
        //封装对象,放在ModelAndView中
        mv.addObject("msg","TestController1");
        
        ////封装要跳转的视图,放在ModelAndView中
        mv.setViewName("test");
        
        return mv;
        
    }
}

4.在配置文件springmvc-servlet中加入下面的语句,将Controller类放入IOC中

<bean id="/test" class="top.moesy.controller.ControllerTest1"/>

5.编写一个测试用jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Test</title>
</head>
<body>
    ${msg}
</body>
</html>

可能遇到的问题

1.web可能没有引入jar包

​ 在File-Project Structure-Artifacts中找到自己的项目,在右边WEB-INF下的lib中引入jar包

2.Controller的接口是自带的,不需要自己配

​ 我觉得这一条可能只会是给我自己看的,记一次奇妙的出错

Last modification:November 25th, 2020 at 01:58 pm