zoukankan      html  css  js  c++  java
  • java之SpringMVC的controller配置总结

    先在springmvc-servlet.xml文件作如下配置(注解开发controller)

    <?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-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- don't handle the static resource --> <mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- scan the package and the sub package --> <context:component-scan base-package="com.eco.controllor"/> <!-- configure the InternalResourceViewResolver视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>

    然后来看看,在有无视图解析器的情况下,转发和重定向的实现

    @Controller//定义这是一个控制器
    public class CController {
        @RequestMapping("/hello1")//浏览器访问路径
        public ModelAndView hello1() {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg", "world");//msg可以用el表达式在下面的页面写出来
            mv.setViewName("01.jsp");//没有视图解析器到根目录的jsp
            //mv.setViewName("hello");//有视图解析器到web-inf下的jsp
            return mv;
        }
    
        @RequestMapping("/hello2")
        public String hello2() {
            return "hello";//转发:使用视图解析器就转到web-inf下的jsp
            //return "redirect:hello1";//重定向:视图解析器到hello1,然后hello1转到web-inf下的hi.jsp
            //return "hello.jsp";//转发:不使用视图解析器转到根目录下的jsp
            //return "forward:01.jsp";//转发:不使用视图解析器根目录下的jsp
            //return "redirect:01.jsp";//重定向:不使用视图解析器根目录下的jsp
        }
        
        @RequestMapping("/hello3")
        public void hello3(HttpServletRequest req,HttpServletResponse resp) throws Exception{
    req.setAttribute(
    "a", "就是我");//a可以用el表达式在下面的页面写出来
    req.getRequestDispatcher("01.jsp").forward(req, resp);//请求转发到根目录下的jsp--不需要视图解析器
    //resp.sendRedirect("01.jsp");//请求重定向到根目录下的jsp--不需要视图解析器 } }

    看完页面跳转,下面再来看看数据的处理(表单)

        @RequestMapping("/hello4")
        //http://localhost:8080/webmvc/hello4?name=eco
        public String hello4(String name){
            System.out.println(name);
            return "hello";
        }
        @RequestMapping("/hello5")
        //http://localhost:8080/webmvc/hello5?name=eco&pwd=112313
        //User类的成员变量和域名称一样
        public String hello5(User user){
            System.out.println(user);
            return "hello";
        }
        @RequestMapping("/hello6")
        //http://localhost:8080/webmvc/hello6?name=eco
        public String hello6(String name,Model model){
            model.addAttribute("username", name);//这个username可以在下面的jsp页面用el表达式写出来
            System.out.println(name);
            return "hello";
        }
  • 相关阅读:
    nginx: [emerg] the size 10485760 of shared memory zone "cache_one" conflicts with already declared size 0
    ruby 删除文件夹(包括文件夹中的文件夹和文件)
    nisi 脚本示例
    将node-expat扩展编译至node.exe中
    将odbc扩展编译至nodejs程序集中
    微信小程序数据传递基本
    Java环境配置
    Angular环境配置
    mysql中常用的数据类型
    html中a标签的4个伪类样式
  • 原文地址:https://www.cnblogs.com/eco-just/p/7882016.html
Copyright © 2011-2022 走看看