您的位置:首页 > 编程语言 > Java开发

SpringMvc——进行注解开发的基本配置

2016-03-22 10:03 489 查看
入手文章,spring大神请绕行。

零,jar包引入

这个是我的myeclipse项目中的截图,如果使用idea进行开发的话,记得要在libraries中引入servlet-api的jar包,不然debug的时候会报错。

一,配置前端控制器

             <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

<!--配置前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器,适配器等)
如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)
-->
<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:访问以.action结尾的由DispatcherServlet解析
第二种:/,所有访问的地址都由DispatcherServlet进行解析,
对于静态文件的解析需要配置不让DispatcherServlet进行解析
使用此种方法可以实现restful风格的url
第三种:/*,这种配置不对,使用这种配置,最终需要转发到jsp页面时候,
仍然由DispatcherServlet解析jsp,不能根据jsp找到handler,会报错
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

    因为我这里暂时木有用到spring的配置,所以现在只加上springmvc的必须配置。

二,spring MVC的配置

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

<mvc:annotation-driven/>
<!-- 自动扫描的包 -->
<context:component-scan base-package="cn.itcast.ssm.controller"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

         这里,注意前缀跟后缀的配置,在最后返回具体的视图的时候,会去/WEB-INF/jsp/+"你controller里面返回的字符串"+.jsp这里找你的资源,如果前缀或后缀配置错误,就404了(个人猜测,因为刚开始我的前缀配置错了,结果404了)。

三,controller中URL的配置及访问地址

    对于Controller类中要访问的方法,使用RequestMapping进行一个定位,类上的RequestMapping可以进行标识,也可以不进行标识。

        

       对应的浏览器中访问地址:http://localhost:8080/SpringMvcTest/hello/mvc。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: