Spring3 mvc hello world demo

 

Spring3mvc hello world demo

工程目录结构图:


其lib下的包是所必须的构建spring mvc 所必须的最少的jar 包。

 

Web 应用的部署描述文件web.xml 内容如下:

<?xml version="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0">

<display-name>SpringMVC Application</display-name>

 

    <servlet>

        <servlet-name>springmvc-dispatcher</servlet-name>

        <servlet-class>

                      org.springframework.web.servlet.DispatcherServlet

                </servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>springmvc-dispatcher</servlet-name>

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

    </servlet-mapping>

 

    <context-param>

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

        <param-value>/WEB-INF/springmvc-dispatcher-servlet.xml</param-value>

    </context-param>

 

    <listener>

        <listener-class>

                      org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

 

</web-app>

 

其中listener 用来监听web 应用的启动消息,并加载context-param 所在的spring bean 的配置文件。servlet 和 servlet-mapping 用来处理http 的请求。web-app 为web 应用的部署xml的根节点。Springmvc 的配置必须有 一个listener和context-param ,servlet 和 servlet-mapping。

 

 

springmvc-dispatcher-servlet.xml  spring bean 的配置文件用来在web应用启动时,加载所需的信息。springmvc-dispatcher 为web.xml 中servlet的name。spring bean的配置文件名为servlet的名字(.DispatcherServlet name)-servlet.xml

配置如下:

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="

        http://www.springframework.org/schema/beans    

       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

    <context:component-scanbase-package="my.common.controller" />

 

    <bean

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

        <property name="prefix">

            <value>/WEB-INF/pages/</value>

        </property>

        <property name="suffix">

            <value>.jsp</value>

        </property>

    </bean>

 

</beans>

图如下:


其中context:component-scan用来指明所需要扫描的包,主要用来对注解的扫描,以便通过反射来进行相应的处理。

InternalResourceViewResolver为视图解析器,主要用来对视图,(jsp)文件。

Spring mvc bean 配置一般都有一个两个设置。

 

@Controller

@RequestMapping("/welcome")

public class HelloController {

 

    @RequestMapping(method = RequestMethod.GET)

    public String printWelcome() {

        return "hello";

 

    }

 

}

 

此为一个控制器类,其中”/welcome”为用户在浏览器中所输入的地址,即请求地址url,方法为get ,对于浏览器中的http ,其默认的方法为get 方法。

“hello” 为请求所对应的返回的视图:如下:在pages 文件夹中。

<html>

<body>

    <h1>Message : spring hello world</h1>  

</body>

</html>

 

通过在工程名HelloWorld 上点击右键,再点击run in server 运行结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值