IDEA Maven 三层架构 二、运行 springMVC

运行 SpringMVC

首先要理解 SpringMVC 应用程序的入口是配置文件 web.xml,其路径为“src/main/webapp/WEB-INF/web.xml”,经过它再去关联 SpringMVC 的配置文件 springmvc-config.xml。
所涉及文件以下图:javascript

Markdown

此处列举了 web.xml 最精简的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="
            http://xmlns.jcp.org/xml/ns/javaee
            http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd
            "
        id="WebApp_ID"
        version="3.1">
 
    <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*:/config/springmvc-config.xml,
                classpath*:/config/datasource.cfg.xml,
                classpath*:/config/activiti.cfg.xml,
                classpath*:/config/mybatis.cfg.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>

此处列举了最精简的 springmvc-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
 
<!-- 在 xsi:schemaLocation 配置中 -->
<!-- 有些 XML 属性会要求.xsd 文件带上版本号。 -->
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://mybatis.org/schema/mybatis-spring
    http://mybatis.org/schema/mybatis-spring.xsd
    ">
 
    <!-- 编译时扫描必要的包 -->
    <!-- 控制器 -->
    <context:component-scan base-package="com.ruanxi.controller"/>
    <!-- 业务逻辑层,自动装配(业务逻辑实现) -->
    <!--<context:component-scan base-package="ruanxi.queen.service"/>-->
    <!-- 自定义配置类(XML配置) -->
    <!--<context:component-scan base-package="ruanxi.queen.config"/>-->
 
    <!-- 处理指定目录下的静态资源,指定哪些资源不要走控制器。需注意:若不定义 mvc:resources,则也不须要 mvc:annotation-driven -->
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <mvc:resources mapping="/*.html" location="/"/>
 
    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/View/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
 
</beans>

配置 Tomcat 服务器

基本的建立参考:【me】IDEA 配置 Tomcat 精要.note,或者(连接分享)html

针对“Server”选项卡的配置须要肯定几个值:java

  • web pom.xml 中 finalName 的值,好比叫“MonkeyPackage”
  • 端口号,好比叫“8501”

如图所示:web

Markdown

Markdown

针对“Deployment”选项卡的配置:spring

一方面注意选择带 exploded 的 war 包选项,另外一方面要填写 Application context,其值等于 web pom.xml 的 finalName。spring-mvc

Markdown

热部署:在开发阶段,务必确保这里选择的是 exploded 格式,这样在“Server”栏的“On frame deactivation”处才有选项“Update classes and resources”,从而开启热部署,它主要表如今当 JSP 页面有变动时能及时经过刷新反映到页面上,不然就要从新编译一次,这就太浪费时间了。tomcat

建立控制器

在 web 组件里,于 src/main/java 中建立 java package,包名为:com.ruanxi.controller,需提醒的是这个包名须要在 springmvc-config.xml 的 component-scan 中注册的。服务器

建立一个控制器,好比名称为:BaseController。mybatis

@Controller
@RequestMapping(value = "/Base")
public class BaseController {
 
    @RequestMapping(value = "/Index", method = RequestMethod.GET)
    public String Index() {
        return "Base/Index";
    }
}

建立视图页面

在 web 组件里,于 src/main/webapp/WEB-INF/View 中建立 JSP 页面,按照必定的约定:以控制器的映射名做为文件夹名称,以方法的映射名做为 JSP 文件名称。mvc

Markdown

客户端的请求要想获得服务端的响应,就必须与映射名相对应,既如此,不如干脆把 jsp 页面的命名也和映射名统一块儿来,勉得额外去思考其余的名字。

运行页面

Markdown

须要注意,JSP 页面的加载并非直接请求而来,在 MVC 框架里是先经过控制器的解析再转发呈现的。

相关文章
相关标签/搜索