Spring MVC 02:请求转发重定向、跨服务器文件上传

一、请求转发重定向

/**
	 * 测试请转发和重定向
	 * 请求中携带参数,参数存放Attribute里,转发过来后可以拿到Attribute的值
	 * 而重定向过去不存值,相当于新的请求(跳转新的网址)
	 */
	//测试请求转发
	@RequestMapping("zf")
	public String test5(HttpServletRequest reqeust){//需要往域里存东西
		reqeust.setAttribute("str","Hello转发");//左边冒号里是名字,右边是内容
		//绝对路径
		//相对路径
		return "forward:result";//没有从根目录/的,直接写同类中的其他地址的相对路径
	}
	//测试重定向
	@RequestMapping("cdx")
	public String test6(HttpServletRequest reqeust){
		reqeust.setAttribute("str","Hello重定向");//左边冒号里是名字,右边是内容
		return "redirect:/test/result";//重定向的关键字是redirect
	}
	//目标位置
	//接收转发和重定向的全部结果并相应到页面上
	@RequestMapping("result")
	@ResponseBody
	public String result(HttpServletRequest request){
		String result=request.getAttribute("str")+"";//获得请求域里面的值object强制转换成string//不能直接.toString(),会空指针异常
		return "Request域中的str的值:"+result;
	}

 结果:
    //http://127.0.0.1:8090/zf

不会跳转到result地址,但有值


    //http://127.0.0.1:8090/cdx

会跳转到result的地址但没有值


    //http://127.0.0.1:8090/result

总结:

转发关键字:forward

重定向关键字:redirect

跨服务器文件上传

一个服务器=一台电脑

单独做文件服务器,跨服务器上传。

借助于Jersey技术

jar包在maven的pom.xml中可以直接配置

<!-- 文件上传 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--跨服务器上传 -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.19</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.19</version>
        </dependency>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<!-- Begin##01、添加SpringBoot的parent支持##Begin -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	<!-- End##01、添加SpringBoot的parent支持##End -->


	<groupId>demo.springboot</groupId>
	<artifactId>cc02helloboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<!-- 连接池 -->
		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp-spring</artifactId>
			<version>0.8.0.RELEASE</version>
		</dependency>
		<!-- Begin##02、添加SpringBoot的Web支持##Begin -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- End##02、添加SpringBoot的Web支持##End -->
		<!-- Begin##添加JSP支持##Begin -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- End##添加JSP支持##Begin -->

		<!-- 文件上传 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!--跨服务器上传 -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			<version>1.19</version>
		</dependency>

		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-core</artifactId>
			<version>1.19</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- 资源文件拷贝插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 设置jdk版本 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- Begin##03、添加SpringBoot的插件支持##Begin -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- End##03、添加SpringBoot的插件支持##End -->
		</plugins>
	</build>
</project>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值