从头搭建Spring MVC

1.拷贝jar文件

从头搭建Spring MVC

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中工程的名称)

 

 

 

 

 

 

 

 

上一篇:数据分析(8):Series介绍


下一篇:ajax请求完之前的loading加载