SpringMVC编程<二>

来源:互联网 发布:matcher java 编辑:程序博客网 时间:2024/06/07 16:08

SpringMVC编程<二>

多业务方法控制器:它是现在推荐使用的注解方式的前身技术

这个类要继承MultiActionController(已经过时了)

这里写图片描述

public class MultiContorllerDemo extends MultiActionController {    public void one() {        System.out.println("这是一个空参方法...是不行的");    }    // 要求带HttpServletRequest request,HttpServletResponse response    public void two(HttpServletRequest req, HttpServletResponse resp) {        System.out.println("业务方法2: 参数必须带原生的 request和response");    }    public String  three(HttpServletRequest req, HttpServletResponse resp,User user) {        System.out.println("业务方法2: 参数必须带原生的 request和response,还可以带其他参数");        System.out.println("User:" + user);        //结果页面--配置文件中配置        return "three";    }    public ModelAndView  four(HttpServletRequest req, HttpServletResponse resp,User user) {        System.out.println("业务方法2: 参数必须带原生的 request和response,还可以带其他参数");        //把数据自己封装到结果页面 ModelAndView        ModelAndView mv= new ModelAndView();        mv.addObject("user", user);//封装信息        mv.setViewName("four");//结果资源名称:four        System.out.println("User:" + user);        //结果页面--配置文件中配置        return mv;    }    public void execute(HttpServletRequest req, HttpServletResponse resp) {        System.out.println("默认方法...");    }}

我在web.xml中配置了另一个bean.xml

这里写图片描述
放在src下
这里写图片描述
在hncu.properties配置文件中写导向的结果页面
多方法类中的返回值(String类型),就是在这里匹配结果页面
这里写图片描述

要使用多方法,在bean.xml中配置必要的核心类:
beans.xml

<!-- 启用Spring(ContextLoaderListener核心类)之后,          项目的bean(业务模块或MVC模块),放在哪一个XML中配置都可以,          2者是同一个容器空间     -->     <!-- 如果把bean配置到Spring容器,该核心类,把URL映射成beanName -->     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>    <!-- 解析方法的核心类 -->    <bean id="methodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">        <!-- 没有指定,就指定默认 -->        <property name="defaultMethodName" value="execute"></property>        <!-- 前面的href就要在?后面加 method=two,也可以改成abc -->        <property name="paramName" value="method"></property>    </bean>    <bean name="/multi" class="cn.hncu.v3.MultiContorllerDemo">        <!-- 一定要配置一个帮我们解析方法的 核心类-->        <property name="methodNameResolver" ref="methodResolver"></property>    </bean>

演示调用:
这里写图片描述
sp/:代表在web.xml中配置了,这个路径下都拦截;
multi:代表MultiContorllerDemo的映射路径名,在beans.xml配置了;
?号后面的method是代表你要调用的方法名;可以在配置文件中更改,改成abc也行
可以在后面传递参数。

现在使用的注解技术Annotation

先写个第一版本:AnnoOneContoller

这里写图片描述
需要在配置文件中加载核心类:

<!--  用于处理注解URL核心类 ,识别用了注解的bean -->     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>

将类加载为一个bean

<bean id="one" class="cn.hncu.anno.AnnoOneContoller"></bean>

第二个版本可以给类加个子路径:

@Controller@RequestMapping(value="/anno2")//带前缀路径public class AnnoTwoContoller {    @RequestMapping(value="/anno_three")    public String two(){        System.out.println("AnnoTwoContoller中的anno_two,带前缀的路径");        return "anno_three";    }}

配置bean

<bean id="two" class="cn.hncu.anno.AnnoTwoContoller"></bean>

也可以不配置单独的bean,可以直接配置一个自动扫描:

<!-- 自动扫描,帮我们把指定包中的所有bean扫描出来,并创建到Spring容器中 -->    <context:component-scan base-package="cn.hncu"></context:component-scan>

演示:
这里写图片描述

测试后发现ModelAndView用2个xml会404!!

原创粉丝点击