使用Maven建立的springmvc工程發佈到tomcat

使用Maven建立的springmvc工程發佈到tomcathtml

一、建好tomcat環境:(點擊add, 選擇Tomcat)java

    (若是列表中沒有Apache Tomcat,多是由於eclipse缺乏插件,能夠:node

  1. Help > Install New Software
  2. Select the neon site: http://download.eclipse.org/releases/neon
  3. Expand the 'Web, XML, and Java EE Development' node
  4. Select and install 'JST Server Adapters' and 'JST Server Adapters Extensions'
  5. Window > Preferences > Server > Runtime Environments > Add)

默認會同時新建一個tomcat serverweb

二、建立一個Maven Project, 選擇類型爲webappspring

三、雙擊pom.xml, 添加依賴tomcat

四、修改maven project的properties中的project Facetsmvc

勾上Dynamic Web Moduleapp

點擊futher configuration available...   修改Content directory爲:src/main/webappeclipse

選擇工程的properties,找到Deployment Assembly, 能夠看到webapp

再點擊Add,選擇Java Build Path Entries,將Maven的包添加到運行時。(不然可能在Eclipse調試時,加時過程當中找不到org.springframework.web.servlet.DispatcherServlet)

五、修改web.xml

<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">

   <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

六、WEB-INF下加入HelloWeb-servlent.xml

<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="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

七、在src/main/java下新建com.tutorialspoint.HelloController

package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{ 
   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }
}

八、在WEB-INF下新建jsp目錄,放入hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</htm

、右擊右下腳的Tomcat Server, 選擇Add and Remove

將工程添加到server

四、run on server

相關文章
相關標籤/搜索