基于注解配置的springMVC小案例
2016-03-03 22:50
489 查看
基于注解配置的springMVC小案例
编程步骤
step1,将spring mvc相关的jar文件添加到WEB-INF\lib下。
step2,添加spring的配置文件(springmvc.xml)。
step3,配置DispatcherServlet(web.xml)
注:
DispatcherServlet的初始化方法在执行时,会启动spring容器。
step4,Controller
step5,hello.jsp
step6,完成springmvc.xml配置。
a.添加组件扫描
<context:component-scan base-package="controller"/>
b.配置spring mvc注解扫描
<mvc:annotation-driven/>
注:
组件扫描只针对@Component,@Service,@Controller,@Repository
如果要让@RequestMapping注解起作用,需要配置mvc注解扫描。
c.配置视图解析器
代码示例:
HelloController.java
web.xml
hello.jsp
编程步骤
step1,将spring mvc相关的jar文件添加到WEB-INF\lib下。
step2,添加spring的配置文件(springmvc.xml)。
step3,配置DispatcherServlet(web.xml)
注:
DispatcherServlet的初始化方法在执行时,会启动spring容器。
step4,Controller
step5,hello.jsp
step6,完成springmvc.xml配置。
a.添加组件扫描
<context:component-scan base-package="controller"/>
b.配置spring mvc注解扫描
<mvc:annotation-driven/>
注:
组件扫描只针对@Component,@Service,@Controller,@Repository
如果要让@RequestMapping注解起作用,需要配置mvc注解扫描。
c.配置视图解析器
代码示例:
HelloController.java
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 二级控制器: * 负责业务逻辑的处理。 * 1.不用实现Controller接口 * 2.在一个Controller类里面,可以添加 * 多个处理方法 * 这些方法的方法名可以自定义,返回值 * 可以是ModelAndView或者是String。 * http://ip:port/springmvc02/hello.do */ @Controller("hc") public class HelloController { @RequestMapping("/hello.do") public String toHello(){ System.out.println("HelloController的toHello方法..."); return "hello"; } }springmvc.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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 配置组件扫描 --> <context:component-scan base-package="controller"/> <!-- 配置spring mvc注解扫描 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- DispatcherServlet的初始化方法 在执行时,会启动spring容器。 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
hello.jsp
<h1>Hello,SpringMVC!</h1>
相关文章推荐
- spring 源码如何导入到eclipse
- Java 服务器端修改apk并重新打包签名
- Java与模式:装饰(Decorator)模式
- Java 服务器端修改apk并重新打包签名
- 错误: 编码GBK的不可映射字符
- Java 堆排序
- 《Java 2 实用教程》读书笔记(二)
- Eclipse介绍
- 带有行标题栏的表格
- 倒排索引算法的Java实现
- java调用kettle向job(任务)和transformation(转换)传递参数实例
- Spring学习之AOP
- 学习Java第一卷--态度的转变
- org.eclipse.wst.xsl.jaxp.debug.invoker.TransformationException
- Eclipse不能导入项目解决方案
- Java基础知识之一注释
- myeclipse如何连接mysql数据库
- Spring MVC 文件下载过程中 出现下载文件乱码
- [转载] Java BigDecimal大数类
- 【java基础】--Java枚举