你的位置:首页 > 软件开发 > Java > 基于注解配置的Spring MVC 简单的HelloWorld实例应用

基于注解配置的Spring MVC 简单的HelloWorld实例应用

发布时间:2015-09-01 10:00:05
2.1 问题使用注解的方式重构helloworld应用案例。2.2 方案1. @RequestMapping注解应用@RequestMapping可以用在类定义和方法定义上,它标明这个类或方法与哪一个客户请求对应。实例代码如下: @RequestMapping("/ ...

基于注解配置的Spring MVC 简单的HelloWorld实例应用

 

2.1 问题

使用注解的方式重构helloworld应用案例。

2.2 方案

1. @RequestMapping注解应用

@RequestMapping可以用在类定义和方法定义上,它标明这个类或方法与哪一个客户请求对应。实例代码如下:

 

2. 开启@RequestMapping注解映射,需要在Spring

<mvc:annotation-driven/>

4. 为了使@Controller注解生效,需要在Spring的

 <context:component-scan base-package="com.souvc.controller"/>

步骤二:增加HelloController类

使用注解的方式,修改HelloController类,代码如下所示:

package com.souvc.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/day01")public class HelloController {  @RequestMapping("/hello.form")  public String execute() throws Exception {    return "hello";  }}

步骤三:spring-mvc.

在spring-mvc.

<?  ="http://www.springframework.org/schema/context"   ="http://www.springframework.org/schema/jee"   ="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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">     <context:component-scan base-package="com.souvc.controller"/>    <mvc:annotation-driven/>    <!-- 定义视图解析器ViewResolver -->  <bean id="viewResolver"    class="org.springframework.web.servlet.view.InternalResourceViewResolver">    <property name="prefix" value="/WEB-INF/jsp/" />    <property name="suffix" value=".jsp" />  </bean></beans>
 

原标题:基于注解配置的Spring MVC 简单的HelloWorld实例应用

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录