首页 > 其他 > 详细

处理方法返回值void

时间:2020-02-27 21:16:40      阅读:22      评论:0      收藏:0      [点我收藏+]

标签:void   页面   mvc   cep   response   resolve   

1、默认响应效果:根据请求url寻找相应页面

1.1、配置的视图解析器

    <!--配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

1.2、假定占位符的值为1,则会寻找/WEB-INF/pages/demo/test/1.jsp页面

package com.ly.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("/test/{1}")
    public void test1() {
        System.out.println("test");
    }
}

1.3、会寻找/WEB-INF/pages/demo/test.jsp页面

package com.ly.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("/test")
    public void test1() {
        System.out.println("test");
    }
}

2、转发

package com.ly.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("/test")
    public void test1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    }
}

3、重定向,重定向是一个新的请求,访问不到WEB-INF目录下的页面

package com.ly.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("/test")
    public void test1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //重定向
        response.sendRedirect(request.getContextPath()+"/test.jsp");
    }
}

4、以流的方式输出到页面

package com.ly.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("/test")
    public void test1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("刘阳");
    }
}

 

处理方法返回值void

标签:void   页面   mvc   cep   response   resolve   

原文:https://www.cnblogs.com/liuyang-520/p/12374163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号