注解方式配置springmvc(idea)
在上篇博客的基础上,我只写不一样的地方
上篇博客:https://blog.csdn.net/qq_42370505/article/details/106566293
不同点一:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:bean="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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置扫描包-->
<bean:component-scan base-package="com.lxz.controller"/>
<!--处理静态资源的方式标签(这里是处理servlet的方式)-->
<mvc:default-servlet-handler/>
<!--注解驱动-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
不同点二:
package com.lxz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @auther:
* @creater:
* @time
**/
@Controller
//@RequestMapping("/abc")
public class HelloController {
// 这里的RequestMapping就相当于<bean id="/hellospringmvc" class="com.lxz.controller.HelloController"></bean>
//如果类前没有@RequestMapping("/abc")这里相当于localhost:8080//hello,如果方法前有,则此时为:localhost:8080//abc/hello,
//当然一般情况下都是不在类前加@RequestMapping这个的,为了怕遗忘,如果真的要加可以直接在方法前改为 @RequestMapping("/abc/hello")
@RequestMapping("/abc/hello")
public String hello(Model md)
{
md.addAttribute("msg","你好世界");
return "test";
}
}
其余的基本一样,接下来可以专注写业务逻辑代码了