SpringMvc——进行注解开发的基本配置
2016-03-22 10:03
489 查看
入手文章,spring大神请绕行。
<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的必须配置。
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了)。
对应的浏览器中访问地址:http://localhost:8080/SpringMvcTest/hello/mvc。
零,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。
相关文章推荐
- SpringMvc——进行注解开发的基本配置
- 利用Spring实现数据库读写分离
- Eclipse Run的时候出现"Launching 项目名,has encountered a problem.Error occurred during the budild.’"
- Java类加载器
- Spring Aop实例之xml配置
- eclipse 离线安装 svn
- java基本类型与引用类型
- java 获取前一天
- Spring aop
- java.lang.IllegalArgumentException: column '_id' does not exist
- maven环境下spring4+mybatis3+mysql配置
- 20160322 javaweb 学习笔记--response 重定向
- Spring3.0中的前置通知、后置通知、环绕通知、异常通知
- eclipse 改变workspace及jsp文件的默认编码格式
- Eclipse安装反编译工具查看Jar包源码
- java类加载机制
- Leetcode:Validate Binary Tree 判断一个二叉树是否为二叉搜索树
- adt直接用,集成svn,里边有sources直接关联一下就可以用
- java.lang.IllegalArgumentException: View not attached to window manager
- 手把手教你做JDK环境变量配置