• (二)重定向以及传值


    一、第一种重定向

    • 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;
            
        }
    }

    结果:

  • 相关阅读:
    Element ui表单验证结束后再次打开清空验证信息
    oracle 新建用户 给用户设定权限
    oracle时间问题
    ORACLE 创建作业JOB例子
    ORACLE SQL PLUSE 创建作业 执行作业
    VS发布,签名出错 试图加载该页时出错,路径的形式不合法。
    Oracle 系统权限
    PL/SQL Developer 8.0 创建 Oracle Jobs 的疑惑
    信息系统系项目管理考试技巧
    水晶报表分组统计中如何让字段值居中显示
  • 原文地址:https://www.cnblogs.com/shyroke/p/7772633.html
Copyright © 2020-2023  润新知