SpringMVC(1) helloWorld项目

通过SpringMVC,创建HelloWorld项目

  1. 添加依赖

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>
    
  2. 配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    	<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             id="WebApp_ID" version="3.0">
        <servlet>
            <servlet-name>springDispatcherServlet</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>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    
  3. 在resources下,创建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>
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    
        <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图(路径) -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        
        <!-- 注册-->
        <bean id="/hello" class="com.pual.controller.Hello"></bean>
    </beans>
    
  4. 创建Hello (com.pual.controller.Hello)类

    package com.pual.controller;
    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 Hello implements Controller {
        @Override
        public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws  Exception{
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg","hello springMVC");
            mv.setViewName("hello");
            return  mv;
        }
    }
    
  5. 在WEB-INF下,创建jsp/hello.jsp

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

6.启动服务器测试

常见问题

1.  	Unable to import maven project: See logs for details

问题产生原因之一:
	Maven 与 IDEA 不兼容导致(我的IDEA:2019的,Maven: 3.6.3 )
解决方法:
	本人采用的方法:更换Maven(换成了 3.6.0 )	
1.  	404问题

问题产生原因之一:
	没有导入jar包(可以在out目录下查看是否有lib目录:如下图片所示)
解决方法:
	在project structure (快捷键:ctrl + alt +shift +s)中 Artifacts中 创建lib目录,并导入jar包(如下图片所示)

在这里插入图片描述
在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读