IDEA Maven 三层架构 2、运行 springMVC

运行 SpringMVC

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

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,或者(链接分享)

针对“Server”选项卡的配置需要确定几个值:

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

如图所示:

Markdown

Markdown

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

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

Markdown

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

创建控制器

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

创建一个控制器,比如名称为:BaseController。

@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 文件名称。

Markdown

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

运行页面

Markdown

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

posted @ 2017-09-21 16:38  溪边静禅  阅读(745)  评论(0编辑  收藏  举报