(二)重定向以及传值

一、第一种重定向

  • index.jsp
<body>
<a href="<%=path%>/Usercontroller/redired">重定向controllr</a>
</body>
  • web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>

        </init-param>

    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <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>
</web-app>
  • springmvc-servlet.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: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">


 <!-- 激活@Required @Autowired @Resource等标注-->  
    <context:annotation-config></context:annotation-config>  
    
      <!-- DispatcherServlet上下文,扫描base-package包中的类,并自动加载到spring容器中 -->  
    <context:component-scan base-package="com.shyroke.controller">
    </context:component-scan>  
   
  <!--   启用@Component,@Controller,@Service,@Repository注解驱动 -->  
      <mvc:annotation-driven/>    
     
    <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/views/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean> 
</beans>

 

  • controller
package com.shyroke.controller;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping(value="/Usercontroller")
public class UserController {

    
    @RequestMapping(value="/redired")
    public ModelAndView redired(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("userName", "admin");
        modelAndView.addObject("passWord", "123520");
        RedirectView redirectView=new RedirectView(request.getContextPath()+"/views/success.jsp");
        modelAndView.setView(redirectView);
        
        return modelAndView;
        
    }
}

 

  • success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    
            重定向传值el表达式要加param。变量名
            userName:${param.userName}
            passWord:${param.passWord}
    
</body>
</html>
  •   <%@ page isELIgnored="false" %>  该工程的jsp版本为1.2  默认不开启el表达式,所以该加上此行否则el表达式不生效。
  • 结果

 

 

二、第二种重定向

  •   除了controller层之外其他配置都与第一种相同

package com.shyroke.controller;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping(value="/Usercontroller")
public class UserController {

    
    @RequestMapping(value="/redired")
    public String redired(HttpServletRequest request){
        
        
        String url="/views/success.jsp?userName=user1&passWord=111";
        
        return "redirect:"+url;
        
    }
}

结果:

 

posted @ 2017-11-02 15:49  shyroke、  阅读(5447)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~