一、请求转发重定向
/**
* 测试请转发和重定向
* 请求中携带参数,参数存放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>