Intellij IDEA 2019 + Java Spring MVC + Hibernate Study Notes (2)

Book connected to text

Firstly, according to various Spring MVC tutorial, the establishment of a structural basis, we do not know whether it is reasonable, so first tentatively, then there are problems to solve the problem. Learning new things, not afraid to fall into a pit. . .

 

Others say online query experience to make all the packages under the lib directory under the root directory, copied to the web / WEB-INF / lib, otherwise tomcat can not run, I think it should only have the right address references, should not to be problems. To not replicate, say out of the pit

Lower project configuration, open File-> Project Structure, then select Modules, then according to the FIG operation, set the output directory. I understand the output directory similar to .Net bin directory inside of why this had to configure their own thing, is not it good agreement this directory it?

After the output directory configured, click Apply, and then select the Dependencies tab. Click the + button to configure JARs or directories to D: \ Spring \ lib, is the Lib directory under the project root directory, others are configuring a Web / WEB-INF / classes / lib, I have this did not file in the root lib copy to this directory, so I just want the D: \ Spring \ lib directory do not know will not fall into a pit. Then Libraries also add to the mix, select Tomcat 8.5.43

 After completion of the configuration web.xml under web / WEB-INF directory, mainly modify the interceptor, so that all can intercept requests Spring, following FIG. web.xml features and functionality similar to web.config in .net. 

<?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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <!--<url-pattern>*.form</url-pattern>-->
        <!-- 将拦截器设置为/,拦截所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

下边修改WEB-INF/dispatcher-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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="cn.webdev.javastudy.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      id="internalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

</beans>

<context:component-scan/>下的base-package是配置spring自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描到文件中有@Service,@Component,@Repository,@Controller等这些注解的类,就这些类注册为bean。这里主要是为了处理Controller注解类。

<mvc:default-servlet-handler/>的作用:由于在web.xml修改了拦截器设置,拦截了所有请求,也包括了静态文件,该配置项就是Spring为了处理静态文件而给出的方案,所有请求首先经过该Servlet检查,静态文件就转给Web服务器处理,不是静态文件就转给DispatcherServlet继续处理。

<mvc:annotation-driven>会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个必须的Bean。其他的更多功能暂不详细了解。


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

该配置是视图解析器的路径映射设置,作用就是通过前后缀来让Controller能够找到正确的视图

下面就新建一个Controller,在controller下新建一个HelloSpringMVC.java的文件,返回值为视图的名称。

package cn.webdev.javastudy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloSpringMVC {

    @RequestMapping("/SpringMVC")
    public String SpringMVC(Model model)
    {
        model.addAttribute("welcome","Hello Spring MVC!, This is My First Spring MVC Page!");
        return "hello";//视图文件名,如:hello.jsp
    }
}

然后在/WEB-INF/jsp/下新建一个hello.jsp文件

<%--
  Created by IntelliJ IDEA.
  User: Rick
  Date: 2019/7/25
  Time: 17:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello Spring MVC!</title>
</head>
<body>
${welcome}
</body>
</html>

运行,自动打开浏览器,输入http://localhost:8080/hello/SpringMVC

我的第一个SpringMVC程序运行成功

 

Guess you like

Origin www.cnblogs.com/Ricklee/p/11237838.html