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

基于注解配置的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

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