基于注解的SpringMvc配置说明
来源:互联网 发布:java内部类初始化 编辑:程序博客网 时间:2024/06/05 06:05
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet;在web.xml中的DispatcherServlet配置如下:
<?xml version="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"> <!-- 配置SpringMvc核心 DispatcherServlet --> <servlet> <servlet-name>SpringMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- SpringMvc默认会到WEB-INF目录下寻找一个叫[servlet-name]-servlet.xml的配置文件 --> <!-- 如果需要手动指定配置文件的位置,那就是通过Servlet的初始化参数来设置contextConfigLocation参数的值 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SpringMVC-servlet.xml</param-value> </init-param> <!-- 表示启动容器的时候,初始化该servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 编码过滤 --> <filter> <filter-name>encode</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encode</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>由于上面手动指定了SpringMvc-servlet.xml配置文件的位置,所以在src目录下面建立名称为SpringMvc-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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 注解驱动 --><mvc:annotation-driven /><!-- 扫描包 --><context:component-scan base-package="com.ly.controller" /><!-- 视图解析器 prefix视图页面前缀,suffix视图页面后缀--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/></bean> </beans>至此,一个简单的基于SpringMvc框架的配置已经完成,SpringMvc需要导入的包,网上有很多。根据个人需要去下载后,导入即可;
下面是一个简单测试例子:
package com.ly.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/hello")public class HelloController { @RequestMapping("/index") public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception { ModelAndView modelView = new ModelAndView(); modelView.addObject("msg","Hello Index"); modelView.setViewName("index"); System.err.println("执行index!"); return modelView; } @RequestMapping("/show") public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception { ModelAndView modelView = new ModelAndView(); modelView.addObject("msg","Hello show"); modelView.setViewName("index"); System.err.println("执行show!"); return modelView; }}上面代码中,@RequestMapping声明了方法的请求路径,如果类上面有这个注解,同时方法名上面有这个注解,那么访问路径为类上面的+方法名上面的,例子中为/hello/index.do访问index方法,/hello/show.do访问show方法。modelView.addObject("msg","Hello Index")类似setAttribute("msg","Hello Index");在页面可以直接使用;modelView.setViewName("index");会根据配置文件中的视图解析器,找到相应的JSP页面。例如本例子中为/WEB-INF/JSP/index.jsp
0 0
- 基于注解的SpringMvc配置说明
- SpringMVC基于注解的配置
- 基于注解的SpringMVC配置的详解
- 基于注解的springmvc配置例子
- 基于注解配置简单的SpringMVC+Mybatis
- 基于注解配置的springMVC小案例
- springmvc基于注解的配置方式
- 基于注解的SpringMVC基本配置
- 基于注解的springMVC(配置)
- 七、初学SpringMVC+Mybatis之SpringMVC基于注解的配置
- 基于注解的SpringMVC
- 基于注解的springmvc
- 基于注解的springmvc
- 基于注解的SpringMVC
- 基于注解的SpringMVC
- 基于注解的SpringMVC
- 基于注解的SpringMVC
- 基于注解的SpringMVC
- 平面最近点对问题—分治算法的经典应用
- 硬编码学习(二)
- UVA 10723 Cyborg Genes(LCS)
- 使用过得sql语句展示--会不断更新完善
- 关于stringWithFormat
- 基于注解的SpringMvc配置说明
- Android进阶2之 阴影制作(Shadow)
- IO流字节流copy方法
- contest: Codeforces Round #297 (Div. 2), problem: (B) Pasha and String
- Spring Aop 细节详解
- 那一夜的风情……
- PC获取手机截图、复制文件、安装APK
- tomcat结合Java定时任务工具实现web中的定时任务
- Oracle:数据库原理