Spring 3 MVCのハローワード

24004 ワード

開発ツール:
   MyEclipse  10.0
プロジェクトディレクトリ:
 
Spring3 MVC 之 Hello Word
[http://www.cnblogs.com/rhythmK/]
1.新規プロジェクト:File->New->Web Project  プロジェクト名:SpringMVC 3
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ダウンロードアドレス:http://files.cnblogs.com/rhythmK/SpringMVC3.rar