Myeclipse2016搭建Spring MVC

本文详细介绍如何搭建基于SpringMVC的Web项目,包括创建项目、配置Spring环境、编写Controller及视图展示等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近忙着找实习,听说现在许多公司开发后台都是用spring mvc 而不使用struts了,原因有很多,我觉得最主要是安全性和spring mvc和spring协作起来更简便吧,毕竟是同一间公司的产品。

闲话少说贴代码:

1.创建一个web project。



2.往项目里面添加spring。



在这里的话spring最好不要使用太高版本,因为ide自带的服务器可能版本跟不上导致项目无法运行,我们这里选3.1就好了。



此外不需要的文件去掉,保留需要的,我们这里就不再需要application-context.xml文件了,但保留web.xml,然后修改里面内容。



3.根据需要修改web.xml内容。

<?xmlversion="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>

//配置DispatcherServlet,他相当于神经中枢,是一个中央控制器,所以的请求都要经过它

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

//配置contextConfigLocation的地址,到时候handleMapping可以根据这个地址里面的配置文件找到相应的controller和资源(js,jsp文件等等);

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

4.配置web.xm提到的springmvc-context.xml,先要自己创建一个新的再往里面添加内容。

<?xmlversion="1.0"encoding="UTF-8"?>

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

//配置控制器所在的包,则所有以com.controller开头的都是

    <context:component-scanbase-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 -->

//配置jsp文件所在的位置,最好放到web-inf的目录下面,这样里面的jsp页面只能通过相关的“action”来跳转了,提高了安全性。

    <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"

            id="internalResourceViewResolver">

        <!-- 前缀 -->

        <propertyname="prefix"value="/WEB-INF/jsp/"/>

        <!-- 后缀 -->

        <propertyname="suffix"value=".jsp"/>

    </bean>

//还可以配置js和css,image等静态资源

    <beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

<mvc:resourceslocation="/js/"mapping="/js/**" />

</beans>

5.编写相关的controller。



package com.controller;


import org.springframework.stereotype.Controller;

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

//告诉HandlerMapping自己是一个controller

@Controller

//标记自己controller的名字 

@RequestMapping("/admin")  

public class Test {

//标记服务的名字

@RequestMapping("/test")  

    public String test() {  

        System.out.println("test");  

        return "test"

}

}

6.编写test.jsp,为什么呢,因为服务里面返回的是test,那么ViewResolver视图解析器肯定要根据这个去找相关的视图呀。



7.启动服务器输入地址测试一下。



这里的话地址是服务器地址+项目名称+控制器名+服务名。


好了这个简单的项目就搭建完成了。






















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值