Rhythmk 一步一步学 JAVA(4):Spring3 MVC 之 Hello Word

开发工具:

   MyEclipse  10.0

项目目录: 

 

 [http://www.cnblogs.com/rhythmK/]

1.新建项目:File->New->Web Project  项目名: SpringMVC3

2.添加 Spring 3.0 Core Libraries  以及  Spring 3.0 Web Libraries  包

/webroot/Index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">

</head>

<body>
    This is my JSP page.MVC
    <br>
    <a href="/login/welcome">/login/welcome</a>
    <br>
    <a href="/welcome">/welcome</a>
    <br>
</body>
</html>

 

/webroot/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>MVC</display-name>

    <!--指定spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

 来源: ww.cnblogs.com/rhythmK/

/webroot/WEB-INF/spring-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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 <mvc:annotation-driven /> -->

    <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
    <context:component-scan base-package="Controller.rhythmk.com" />

    <!-- 对模型视图名称的解析,在WEB-INF/view目录下找对应的jsp文件 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/view/" p:suffix=".jsp" />

    <!--放过/scripts下的静态文件 <mvc:resources mapping="/scripts/**" location="/scripts/" 
        /> -->
</beans> 

/webroot/WEB-INF/View/login/hello.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

    <a href="/SpringMVC3/login/welcome">/login/welcome</a>
    <br>
    <a href="/SpringMVC3/welcome">/welcome</a>
    <br> 你好:<%=request.getAttribute("loginUser")%>
    UID:${loginid}
</body>
</html>

 

/webroot/WEB-INF/View/welcome.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

    <a href="/SpringMVC3/login/welcome">/login/welcome</a>
    <br>
    <a href="/SpringMVC3/welcome">/welcome</a>
    <br> Hello Word
</body>
</html>

运行:

http://xjjwwnvjwaqijdb:8080/SpringMVC3/welcome

http://xjjwwnvjwaqijdb:8080/SpringMVC3/login/welcome

 

Demo下载地址: https://files.cnblogs.com/rhythmK/SpringMVC3.rar

 

posted @ 2013-01-31 21:57  Rhythmk  阅读(736)  评论(0编辑  收藏  举报
Rhythmk 个人笔记