Spring3 MVC :Hello world

使用netbeans8建立java web项目HelloSpring,选择Spring 3.2.7。

项目完成时结构如下:
Spring3 MVC :Hello world

run这个项目,浏览器结果为:
Spring3 MVC :Hello world

下面依次列出各个文件的内容。

/web/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    

    <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                   org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

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

</web-app>



/web/WEB-INF/dispatcher-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-scan base-package="letian.spring.controller" /> <!-- 特别注意 -->
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
  
</beans>



/web/WEB-INF/jsp/hello.jsp:

<html>
    <head>
        <title>My first example using Spring 3 MVC</title>
    </head>
    <body>
        <h1>${message}</h1>
    </body>
</html>



/src/java/letian/spring/controller/HelloController.java:


package letian.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class HelloController {
 
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }   

}



参考:

http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/

http://my.oschina.net/xishuixixia/blog/140141









你可能感兴趣的:(Spring3 MVC :Hello world)