您现在的位置是:首页 > 个人日记个人日记
springMVC-02-注解开发
2019-02-09 19:22:01【个人日记】266人已围观
简介基于注解的方式开发springMVC
注解开发
相对于快速开发,只是改变了springmvc-cnfig.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:mvc="http://www.springframework.org/schema/mvc"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"><!--注解扫描--><context:component-scan base-package="com.huangxin.order.controller"/><!--注解驱动--><!--可以自动加载映射器和适配器--><mvc:annotation-driven/><!--视图解析器--><!--可以解析jsp--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><!--自动解析--><property name="suffix" value=".jsp"/></bean></beans>
最后只剩下这三个核心配置
controller.java也改变了
package com.huangxin.order.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controller//表示是一个后端控制器public class HelloAnnotationController {@RequestMapping("/a")public String hello(Model model) {model.addAttribute("hello", "Hello SpringMVC");return "helloAnnotation";}}
注意
1) @RequestMapping("/a")表示访问的地址,也就是http://localhost:8080/a来访问这个地址
2) @Controller让其注册为Bean
3) 返回资源地址也就是/WEB-INF/jsp/helloAnnotation.jsp文件
Tags: JavaWeb
上一篇: mybatis-03-Mapper代理
下一篇: spring-01-快速上手
相关文章
随机图文
评论区
2025-11-09 06:15:05
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信
