处理方法返回值void
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("刘阳"); } }
相识是缘