• 从头搭建Spring MVC


    1.拷贝jar文件

    2.填充Web.xml

    在/WEB-INF/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" xmlns:web="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_2_5.xsd" id="WebApp_ID" version="2.5">

    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value></param-value>

    </context-param>

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <servlet>

    <servlet-name>x</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath*:x-servlet.xml</param-value>

    </init-param>

    </servlet>

    <servlet-mapping>

    <servlet-name>x</servlet-name>

    <url-pattern>/</url-pattern>

    </servlet-mapping>

    </web-app>

     

    context-param:指定applicationContext(bean定义)的位置;

    servlet指定了ServletDispatcher以及servlet配置文件路径,如果是WEB-INF下面,"contextConfigLocation"节点的value采用"/WEB-INF/XX-servlet.xml"形式进行定义;如果是其他文件夹,则采用classpath形式进行指定;前者意味着servlet配置文件路径在WEB-INF下;后者意味着到编译后的文件夹中找配置文件;

     

    3.填充x-servlet

    在resources文件夹或者/WEB-INF/目录下创建x-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:mvc="http://www.springframework.org/schema/mvc"

        xmlns:context="http://www.springframework.org/schema/context"

        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">

        <mvc:annotation-driven />

        <context:component-scan base-package="neusoft.monitor.controller" />

        <!-- jsp视图 -->

        <bean id="jspViewResolver"

            class="org.springframework.web.servlet.view.InternalResourceViewResolver">

            <property name="prefix" value="/WEB-INF/pages/" />

            <property name="suffix" value=".jsp" />

        </bean>

    </beans>

    mvc:annotation-driven,代表通过注解进行设置controller;

    context:component-scan,代表要进行扫描注解的包;

    <bean id="jspViewResolver">定义了如何找到jsp页面;

    4. 编写controller

    @Controller

    public class HomeController {

        @RequestMapping(value = "/home")

        public String showHomePage() {

            return "home";

        }

    }

    编写home.jsp页面

    随便写点什么;

    5. 尝试访问

    敲入网址localhost:8080/myspringmvc/home即可看到home.jsp内容。(myspringmvc是eclipse中工程的名称)

     

     

     

     

     

     

     

     

  • 相关阅读:
    遗传算法求解旅行商(TSP)问题 -- python
    office 小技巧
    oracle创建dblink
    c# equals与==的区别
    两人之间的一些参数
    .net 枚举(Enum)使用总结
    SQL Server 日期的加减函数: DATEDIFF DATEADD
    jquery操作select
    AS3帮助手册
    Razor和HtmlHelper的使用意义
  • 原文地址:https://www.cnblogs.com/xiashiwendao/p/5677262.html
Copyright © 2020-2023  润新知