标签:index beans rect ref 访问 one default std ons
applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.controller"/>
<!-- 开启SpringMVC注解的方式 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置前后缀 -->
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controller
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
/*@RequestMapping("/con") //这里也可以配置访问这整一个Controller路径*/
public class MyController{
/**
* 服务器端跳转/请求转发
* url没有变化,进入视图解析器
* @return
*/
@RequestMapping("/indexA")
public String indexA() {
// TODO Auto-generated method stub
return "index";//默认为"forward:/路径"可以不加forward
}
/**
* 客户端重定向/页面重定向
* 这里使用redirect:路径
* 必须是绝对路径,进入视图解析器
* @return
*/
@RequestMapping("/indexB")
public String indexB() {
// TODO Auto-generated method stub
return "redirect:/jsp/index.jsp";
}
/**
* 服务器给客户端直接响应一个字符串信息
* @return
*/
@RequestMapping("/indexC")
@ResponseBody//加上不进入视图解析,直接显示在页面
public String indexC() {
// TODO Auto-generated method stub
return "这是在页面显示哦";
}
/**
* 使用HttpServletRequest、HttpServletResponse
* 进行服务端跳转
* 这里是不进入视图解析器的
* 路径需要使用绝对路径
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@RequestMapping("/indexD")
public void indexD(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.getRequestDispatcher("jsp/index.jsp").forward(req, resp);
}
/**
* 使用HttpServletRequest、HttpServletResponse
* 进行客户端重定向
* 这里是不进入视图解析器的
* 路径需要使用绝对路径
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@RequestMapping("/indexE")
public void indexE(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
resp.sendRedirect("jsp/index.jsp");
}
/**
* 定义请求方式
* 采用resful风格的POST
* 进行服务端跳转
* @return
*/
@RequestMapping(value="/indexH",method=RequestMethod.POST)
public String indexH() {
// TODO Auto-generated method stub
System.out.println("进入了indexH()");
return "index";
}
/**
* 定义请求方式
* 采用resful风格的GET
* 进行服务端跳转
* @return
*/
@RequestMapping(value="/indexF",method=RequestMethod.GET)
public String indexF() {
// TODO Auto-generated method stub
System.out.println("进入了indeF()");
return "index";
}
/**
* 使用@GetMapping相当于@RequestMapping(value="/indexF",method=RequestMethod.GET)
* 设置请求方式为GET
* @return
*/
@GetMapping("/indexG")
public String indexG() {
// TODO Auto-generated method stub
System.out.println("进入了indexG()");
return "index";
}
/**
* 使用@PostMapping相当于@RequestMapping(value="/indexF",method=RequestMethod.POST)
* 设置请求方式为POST
* @return
*/
@PostMapping("/indexI")
public String indexI() {
// TODO Auto-generated method stub
System.out.println("进入了indexI()");
return "index";
}
/**
* 使用ModelAndView
* 返回一个ModelAndView对象
*
* @return
*/
@RequestMapping("/sel")
public ModelAndView selIndex() {
// TODO Auto-generated method stub
System.out.println("----返回ModelAndView----");
ModelAndView mav = new ModelAndView();
mav.setViewName("/index.jsp");
return mav;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringDemo08</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<!-- 中央处理器or前端控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置文件 -->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<!-- 映射路径 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC__配置视图解析以及服务端重定向和服务端跳转简单代码
标签:index beans rect ref 访问 one default std ons
原文地址:https://www.cnblogs.com/zhangsonglin/p/10932296.html