使用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

相关文章
相关标签/搜索