【spring mvc】java-Spring mvc 简单搭建

【开发日记】java-Spring mvc简单搭建

开发环境

myEclipse +tomcat6 开发-1

   工程文件及jar包

     点击此处

第一步:建立一个Web项目

在myeclipse下创建web工程SpringTest2016.

第二步:导入spring3.2.0的jar包


第三步:前端控制器配置

在\WEB-INF\web.xml中配置前端控制器,这里很关键

代码

<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.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

 

 

load-on-startup:表示servlet随服务启动;

url-pattern:*.action的请交给DispatcherServlet处理。

contextConfigLocation:指定springmvc配置的加载位置,如果不指定则默认加

载WEB-INF/[DispatcherServlet的Servlet 名字]-servlet.xml。

 

具体截图

 

第四步:springmvc的配置

1、 在SpringTest2016中新建一个源文件夹、命名config

2、 新建一个文件,命名为springmvc.xml注意格式为xml

 


 

注意web.Xml里面classpathspringmvc.xml为上面的文件名这里是对应关系,这里填错了会运行不起来


1、复制头代码进去

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.2.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

 

2、配置处理器映射器:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

 

 

3、配置处理器配置

<!--controller配置 -->

<bean name="/hello.action"  class="cn.gxxy.ssm.controller.hello"/>

 

4、配置视图解析器

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

        <propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>

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

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

5、总体截图

 


 

 

第五步: hello处理器开发

1、新建一个hello.java作为处理器

2、编辑hello.java添加servlet

添加对应的包->添加未实现的方法


整个hello.java文件


3、添加对应的jsp文件-目录已经对应/WEB-INF/jsp/

详情在springmvc.xm里面、后缀为.jsp所以这里写个hello就行了

 

 

 

第六步: 运行程序

1、 工程——》运行-

2、 在地址栏输入:http://desktop-b2id923:8080/SpringTest2016/hello.action

效果:

 

 

好了,Spring已经基本搭建完了,最后祝大家Spring搭建成功.

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_陈陆亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值