首页 > 代码库 > 1.SpringMVC部署

1.SpringMVC部署

1.在MyEclipse中部署:(项目结构图)

技术分享技术分享

 web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- Spring MVC配置 -->
    <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>

</web-app>

spring-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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
    <mvc:annotation-driven >
         
    </mvc:annotation-driven>
   
    <!-- 启动包扫描功能,以便注册带有@Controller、@service、@repository、@Component等注解的类成为spring的bean -->
    <context:component-scan base-package="com.springmvc" />
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="viewClass" value="http://www.mamicode.com/org.springframework.web.servlet.view.JstlView"/>
         <property name="prefix" value="http://www.mamicode.com/"/>    <!-- 前缀 -->
         <property name="suffix" value="http://www.mamicode.com/.jsp"/>    <!-- 后缀 -->
     </bean>
</beans>

controller文件:

 1 package com.springmvc;
 2 
 3 
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 @Controller
 8 @RequestMapping("/Test/*") 
 9 public class HelloWorld
10 {
11      //访问地址:http://localhost:8080/Test/returnSuccess
12     @RequestMapping(value = "http://www.mamicode.com/returnSuccess")    //实际访问的url地址
13     public String returnSuccess() {
14         return "success";    //返回Views文件夹下的success.jsp页面
15     }
16 }

 在MyEclipse中,存储源代码的叫工作空间(workplace),在里面可以建不同的项目。针对JavaWeb项目,浏览器访问时需要添加对应的项目,如下图中的springmvc。技术分享

 

1.SpringMVC部署