springmvc -- springMVC笔记2--基于注解的实现
spirngmvc 与spring相似支持以注解的方法实现自己的功能,老何曾经问什么叫做低侵入性的框架,回答是我们要引进框架自己的jar包,这是不可避免的,但是我们只是用他们实现功能,并不去继承jar包中的类,之前的框架中没有注意这个事情,基本都去继承了某个类,这里我们可以通过注解代替继承
创建工程:SpringMVC2、导入jar
web.xml 与helloworld实例的配置相同
spring-servlet.xml
<beans xmlns=”http://www.springframework.org/schema/beans"
xmlns:context=”http://www.springframework.org/schema/context"
xmlns:p=”http://www.springframework.org/schema/p"
xmlns:mvc=”http://www.springframework.org/schema/mvc"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package=”com.firefly.springmvc2.controller”>
<property name="prefix" value="/jspPage/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
说一下这个配置文件中的坑吧,我花了一整天的时间就在这个配置文件这过不去,首先我们已经是决定用注解编程了就不要再写一个controller的bean了
其次mvc:annotation-driven/这个东西比较奇葩,框架要求我们要配置两个东西:HandlerMapping和HandlerAdapter,这两个东西是为了连接注解中的@controller;我在上面也配置了,但是不知道是在哪里出了问题,反正那两个东西没有生效,而我是不知道的,这个语句会自动帮我们配置这两个东西,并且在后面要与ajax结合是还会用到
然后springMVC3与jdk1.8不兼容
4.WelcomeController.java
package com.firefly.springmvc2.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(“/welcome”)
public class WelcomeController{
@RequestMapping("/welcome")
public ModelAndView welcome(HttpServletRequest arg0, HttpServletResponse arg1){
System.out.println("---mvc---");
ModelAndView mv = new ModelAndView();
mv.addObject("welcome","welcome to springmvc2");
mv.setViewName("welcome");
return mv;
}
- }
5.编写jsp
