使用Myeclipse2015構建SpringMVC項目


1.新建web project

2.右鍵項目,給項目添加spring框架如圖,不需要勾選任何一個選項。

3.在WebRoot/WEB-INF目錄下添加web.xml內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
<!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc-servlet.xml</param-value>
      </init-param>
      <!-- <load-on-startup>1</load-on-startup> -->
</servlet> 
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

servlet模塊添加了springmvc的調度器,如果需要改springmvc的配置文件的名字,可以更改springmvc-servlet.xml這個名字為指定名字。

servlet-mapping連接了這個servlet,並能夠定義url-pattern,一般使用/而不是/*。

4.在src目錄下添加springmvc-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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                 
    <!-- scan the package and the sub package -->
    <context:component-scan base-package="com.controller"/> 
    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler /> 
    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />     
    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            id="internalResourceViewResolver">
        <!-- 前綴 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后綴 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

base-package指定你的controller所在的包,prefix和suffix分別指定了view所在的位置和后綴,這個配置文件連接了controller和view。

5.新建controller,所在包應為springmvc-servlet.xml中指定的base-package

結構如下:

內容如下:

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/Hello")
public class HelloController {
@RequestMapping("/hello")
public String hello(){
    return "hello";
}
}

6.新建view,所在位置應為springmvc-servlet.xml中的prefix,后綴為指定后綴,名稱與controller中返回的字符串相同。

內容自擬。

至此,一個springmvc的hello world項目配置完畢。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM