您现在的位置是:首页 > 个人日记个人日记

springMVC-02-注解开发

2019-02-09 19:22:01【个人日记】203人已围观

简介基于注解的方式开发springMVC

注解开发

相对于快速开发,只是改变了springmvc-cnfig.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  7. <!--注解扫描-->
  8. <context:component-scan base-package="com.huangxin.order.controller"/>
  9. <!--注解驱动-->
  10. <!--可以自动加载映射器和适配器-->
  11. <mvc:annotation-driven/>
  12. <!--视图解析器-->
  13. <!--可以解析jsp-->
  14. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="prefix" value="/WEB-INF/jsp/"/>
  16. <!--自动解析-->
  17. <property name="suffix" value=".jsp"/>
  18. </bean>
  19. </beans>

最后只剩下这三个核心配置

controller.java也改变了

  1. package com.huangxin.order.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. @Controller//表示是一个后端控制器
  6. public class HelloAnnotationController {
  7. @RequestMapping("/a")
  8. public String hello(Model model) {
  9. model.addAttribute("hello", "Hello SpringMVC");
  10. return "helloAnnotation";
  11. }
  12. }

注意

1) @RequestMapping("/a")表示访问的地址,也就是http://localhost:8080/a来访问这个地址
2) @Controller让其注册为Bean
3) 返回资源地址也就是/WEB-INF/jsp/helloAnnotation.jsp文件

Tags: JavaWeb  

上一篇: 网页抓取工具

下一篇: mybatis-05-动态SQL

评论区

    2024-04-23 20:36:06

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我