Spring初学者的每一步之配置Spring

以前都有接触过Spring但是都没有很系统的去学习过,到现在重新去拿起Spring的时候,发现都忘的差不多了

好吧,又重新来过,这次呢,从头开始,希望能够学得系统一点!

现在,开始配置Spring


1.首先要导入springframework的包,在这里我用的是最新的Spring4.2.3 ,这里面没有包含commons-loggin.jar

这个包,所以这个包需要自己导入进去

2.导入完成后,开始配置web.xml,

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <description>加载/WEB-INF/目录下的dispatch-servlet.xml作为Spring MVC的配置文件</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatch-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

3.配置完web.xml之后,在WEB-INF下现金一个xml文件,dispatch-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" 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-4.2.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.2.xsd 
">
<!--
        使Spring支持自动检测组件,如注解的Controller
    -->
<context:component-scan base-package="com.webtest.controller" />


<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

这段代码中,spring会自动去扫描com.webtest.comtroller里带有@controller,@Service,@repository,@component的类注册为Spring的bean

viewResolver是对视图名称的解析,在请求返回时加上前缀和后缀,这样能够使controller返回一个页面

4, applicationContext.xml暂时没有写东西,后面会试着看能不能不用这个xml就能够使用bean,接下来就直接写controller了

package com.webtest.controller;


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


@Controller
public class TestController {


@RequestMapping(value="/index")
public String index(){
return "MyJsp";
}
}

在controller前加上@controller标志,标志一个controller

@RequestMapping:这个是用来处理请求地址映射的注解,可用于类上,用在类上的时候,表示类里面所有响应请求的方法都是以该地址作为父路径

5,好了,到现在,一个简单的spring就算是搭建完成了,直接运行就能够看到结果 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值