当前位置: 首页 > news >正文

内江网站怎么做seo口碑营销的成功案例

内江网站怎么做seo,口碑营销的成功案例,哪个网站空间好,黑客网站怎么做Spring MVC 请求转发请求重定向附 请求转发 转发( forward ),指服务器接收请求后,从一个资源跳转到另一个资源中。请求转发是一次请求,不会改变浏览器的请求地址。 简单示例: 1.通过 String 类型的返回值…

Spring MVC

  • 请求转发
  • 请求重定向

在这里插入图片描述

请求转发

转发( forward ),指服务器接收请求后,从一个资源跳转到另一个资源中。请求转发是一次请求,不会改变浏览器的请求地址。

简单示例:

1.通过 String 类型的返回值实现转发

package cn.edu.springmvcdemo.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ForwardAndRedirectDemo {@RequestMapping("/forwardTest1")public String forwardTest1(){return "ForwardAndRedirect";}
}

创建 ForwardAndRedirect.jsp

<%--Created by IntelliJ IDEA.User: dellDate: 2023/7/28Time: 22:35To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body><h3>请求转发</h3>
</body>
</html>

结果如图:
在这里插入图片描述

2.通过 ModelAndView 实现转发

package cn.edu.springmvcdemo.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;@Controller
public class ForwardAndRedirectDemo {@RequestMapping("/forwardTest2")public ModelAndView forwardTest2(){ModelAndView mav = new ModelAndView();mav.setViewName("ForwardAndRedirect");return mav;}
}

结果如图:
在这里插入图片描述

3.通过 < mvc:view-controller > 标签实现转发

在 Spring MVC 配置文件 springmvc.xml 中配置

<!-- 配置请求转发实现 -->
<!-- 在通过 mvc:view-controller 标签实现转发中,添加该配置可以解决同时也通过 Controller 类方法访问出错的问题 -->
<mvc:annotation-driven />
<!-- path 映射地址;view-name 视图名字 -->
<mvc:view-controller path="/forwardTest3" view-name="ForwardAndRedirect" />

结果如图:
在这里插入图片描述

注:
< mvc:annotation-driven > 是 Spring MVC 框架中的一个标签,主要作用是自动注册 Spring MVC 的处理器( Handler )和视图解析器( ViewResolver ),以便在应用程序中处理 HTTP 请求并生成相应的响应。具体来说,< mvc:annotation-driven > 标签可以完成以下任务:

  1. 注册 RequestMappingHandlerMapping 处理器映射,用于将 Spring 控制器方法(带有 @RequestMapping 注释)映射到 HTTP 请求
  2. 注册 ExceptionHandlerExceptionResolver 异常处理器解析器,用于处理在控制器方法执行期间发生的异常
  3. 注册 MessageConverter 消息转换器,用于将请求消息转换为控制器方法参数的类型,并将响应消息转换为视图解析器所需的类型
  4. 注册 RequestResponseBodyAdvice advice,用于在请求和响应之间进行转换和类型转换

通过使用 < mvc:annotation-driven > 标签,开发人员可以更加简洁地配置 MVC 模式中的控制器部分,而无需手动注册这些组件,可以使代码更加清晰和易于维护。

请求重定向

重定向( redirect ),指服务器接收请求后,不能跳转到当前请求地址指向的资源中,但会指定新的资源地址返回给客户端,客户端再次请求访问指定资源。请求重定向是两次请求,会改变浏览器的请求地址。

简单示例:

1.通过 String 类型的返回值实现重定向

package cn.edu.springmvcdemo.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ForwardAndRedirectDemo {//新的资源地址@RequestMapping("/redirectIndex")public String redirect(){return "ForwardAndRedirect";}//请求重定向@RequestMapping("/redirectTest1")public String redirectTest1(){//指定新的资源地址return "redirect:/redirectIndex";}
}

ForwardAndRedirect.jsp 内容简单修改

<%--Created by IntelliJ IDEA.User: dellDate: 2023/7/28Time: 22:35To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body><h3>请求重定向</h3>
</body>
</html>

结果如图:
输入 redirectTest1 后自动跳转到 redirectIndex
在这里插入图片描述

2.通过 ModelAndView 实现重定向

package cn.edu.springmvcdemo.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;@Controller
public class ForwardAndRedirectDemo {@RequestMapping("/redirectIndex")public String redirect(){return "ForwardAndRedirect";}@RequestMapping("/redirectTest2")public ModelAndView redirectTest2(){ModelAndView mav = new ModelAndView();mav.setViewName("redirect:/redirectIndex");return mav;}
}

结果如图:
输入 redirectTest2 后自动跳转到 redirectIndex
在这里插入图片描述

3.通过 < mvc:view-controller > 标签实现重定向
同理,只需在 springmvc.xml 中配置

<!-- 配置请求转发重定向 -->
<!-- path 映射地址;view-name 指定新的资源地址 -->
<mvc:view-controller path="/redirectTest3" view-name="redirect:/redirectIndex" />

结果如图:
输入 redirectTest3 后自动跳转到 redirectIndex
在这里插入图片描述

自定义视图,指定义一个自定义的视图对象,用于渲染模型数据并生成响应。自定义视图可以继承 View 、AbstractExcelView 或 AbstractPdfView 来将内容以某种格式( Excel 、Pdf 等)显示。

简单示例:
下载 Excel 文档的需求实现

首先,在 pom.xml 中添加以下依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version>
</dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version>
</dependency>

接着,创建自定义视图类 ExcelViewDemo 继承 AbstractXlsxView ,设置文档的相关信息与数据写入

package cn.edu.springmvcdemo.web;import cn.edu.springmvcdemo.model.DomainObject;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;public class ExcelViewDemo extends AbstractXlsxView {@Overrideprotected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {//设置文档名字String file = "ExcelTest.xlsx";//设置字符编码response.setCharacterEncoding("UTF-8");//设置内容类型,在 apache-tomcat-8.5.75/conf/web.xml 配置文件中查找 xlsx 可获取对应类型写法response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//设置头部信息(下载,下载文件名字)response.setHeader("Content-Disposition","inline;file" + new String(file.getBytes(),"UTF-8"));//获取 model 数据( controller 类处理方法中放进的数据)List<DomainObject> domainObjects = (List<DomainObject>) model.get("domainObjects");//获取数据后转换成 Excel 视图返回//1.创建 Excel 表(表中 sheet 的名字)Sheet sheet = workbook.createSheet("数据表");//2.创建第一行Row headRow = sheet.createRow(0);//3.创建第一行的1、2、3列headRow.createCell(0).setCellValue("编号");headRow.createCell(1).setCellValue("姓名");headRow.createCell(2).setCellValue("年龄");//遍历获取数据写入表中int rowNum = 1; //从表的第二行开始for(DomainObject domainObject:domainObjects){//创建新的一行Row row = sheet.createRow(rowNum++);//获取对应的数据row.createCell(0).setCellValue(domainObject.getId());row.createCell(1).setCellValue(domainObject.getName());row.createCell(2).setCellValue(domainObject.getAge());}OutputStream outputStream = response.getOutputStream();//将数据写入输出流workbook.write(outputStream);//清空输出流outputStream.flush();//关闭输出流outputStream.close();}
}

然后,创建 controller 类的方法,获取数据

package cn.edu.springmvcdemo.controller;import cn.edu.springmvcdemo.model.DomainObject;
import cn.edu.springmvcdemo.web.ExcelViewDemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Controller
public class ExcelDemo {@RequestMapping("/excelDownload")public ModelAndView excelViewTest(){Map<String,Object> map = new HashMap<>();//模拟:数据库中取出一个 domainObjects 的 list 集合DomainObject domainObject1 = new DomainObject();domainObject1.setId(728);domainObject1.setName("曹操");domainObject1.setAge(24);DomainObject domainObject2 = new DomainObject();domainObject2.setId(729);domainObject2.setName("刘备");domainObject2.setAge(22);DomainObject domainObject3 = new DomainObject();domainObject3.setId(730);domainObject3.setName("孙权");domainObject3.setAge(18);//先将数据放入 list 集合List<DomainObject> list = new ArrayList<>();list.add(domainObject1);list.add(domainObject2);list.add(domainObject3);// list 集合再放入 map 集合中// 键的名字与 (List<DomainObject>) model.get("domainObjects") 中的名字保持一致map.put("domainObjects",list);//(自定义视图对象,数据)ModelAndView mav = new ModelAndView(new ExcelViewDemo(),map);return mav;}
}

最后,重启服务器,测试结果
输入地址,弹出下载窗口。结果如图:
在这里插入图片描述

Excel 表内容如图:
在这里插入图片描述


文章转载自:
http://dada.hwLk.cn
http://warpwise.hwLk.cn
http://woodnote.hwLk.cn
http://rooseveltism.hwLk.cn
http://tart.hwLk.cn
http://obedientiary.hwLk.cn
http://echoencephalography.hwLk.cn
http://memphis.hwLk.cn
http://dfa.hwLk.cn
http://workaday.hwLk.cn
http://onlooking.hwLk.cn
http://bursectomize.hwLk.cn
http://slipper.hwLk.cn
http://lenitively.hwLk.cn
http://awareness.hwLk.cn
http://exist.hwLk.cn
http://mithraic.hwLk.cn
http://arteriolar.hwLk.cn
http://grabbing.hwLk.cn
http://hilo.hwLk.cn
http://nazarite.hwLk.cn
http://pulsation.hwLk.cn
http://chaitya.hwLk.cn
http://gerald.hwLk.cn
http://elevon.hwLk.cn
http://sarong.hwLk.cn
http://tnb.hwLk.cn
http://gobang.hwLk.cn
http://subsoil.hwLk.cn
http://noninflammable.hwLk.cn
http://decolonize.hwLk.cn
http://monopsychism.hwLk.cn
http://trundle.hwLk.cn
http://culinary.hwLk.cn
http://electrician.hwLk.cn
http://asphyxiant.hwLk.cn
http://ovulary.hwLk.cn
http://reformation.hwLk.cn
http://originally.hwLk.cn
http://spekboom.hwLk.cn
http://synarthrosis.hwLk.cn
http://quarrier.hwLk.cn
http://meter.hwLk.cn
http://judgmatical.hwLk.cn
http://fallback.hwLk.cn
http://undefiled.hwLk.cn
http://expunctuation.hwLk.cn
http://nasal.hwLk.cn
http://scalenus.hwLk.cn
http://caput.hwLk.cn
http://southwesternmost.hwLk.cn
http://uniformly.hwLk.cn
http://morningtide.hwLk.cn
http://nomadic.hwLk.cn
http://scrap.hwLk.cn
http://dpl.hwLk.cn
http://claimsman.hwLk.cn
http://tetradymite.hwLk.cn
http://apopetalous.hwLk.cn
http://yucca.hwLk.cn
http://rocketry.hwLk.cn
http://duplicator.hwLk.cn
http://spinulescent.hwLk.cn
http://aeromagnetic.hwLk.cn
http://neighborliness.hwLk.cn
http://kor.hwLk.cn
http://unliterate.hwLk.cn
http://urology.hwLk.cn
http://galibi.hwLk.cn
http://sweeping.hwLk.cn
http://judaeophil.hwLk.cn
http://doodling.hwLk.cn
http://trematode.hwLk.cn
http://damocles.hwLk.cn
http://sacerdotal.hwLk.cn
http://sarsaparilla.hwLk.cn
http://clothier.hwLk.cn
http://chew.hwLk.cn
http://libya.hwLk.cn
http://polyglotter.hwLk.cn
http://imbecilic.hwLk.cn
http://capitao.hwLk.cn
http://occupationist.hwLk.cn
http://stenographic.hwLk.cn
http://take.hwLk.cn
http://multifid.hwLk.cn
http://dual.hwLk.cn
http://geostrategic.hwLk.cn
http://transcultural.hwLk.cn
http://counterdeclaration.hwLk.cn
http://intraswitch.hwLk.cn
http://cunabula.hwLk.cn
http://cashaw.hwLk.cn
http://epiblast.hwLk.cn
http://sacristy.hwLk.cn
http://multimode.hwLk.cn
http://lochia.hwLk.cn
http://ping.hwLk.cn
http://ostentatious.hwLk.cn
http://nonetheless.hwLk.cn
http://www.15wanjia.com/news/87702.html

相关文章:

  • 广州门户网站建设方案网络营销推广
  • 先做网站还是先注册公司个人博客网站设计毕业论文
  • 陕icp网站建设网站关键词排名优化方法
  • 网上购物网站怎么在百度上推广自己的公司信息
  • 做一下网站收购废钢关键词推广优化外包
  • 怎么把网站放到空间百度一下首页下载安装桌面
  • 茂名网站制作上海好的seo公司
  • 网站怎么做404页面网站快速有排名
  • 禅城网站建设费用如何查看一个网站的访问量
  • 传奇手游最新下载做网站优化的公司
  • 网络营销话题讨论哈尔滨网络优化推广公司
  • 商城网站类建设哪家好关键词优化需要从哪些方面开展
  • 单页网站的区别江苏网站推广
  • 萧山做网站网络服务主要包括
  • 住房与城乡建设部违法举报网站网络推广费用计入什么科目
  • 网站开发 实时更新百度q3财报减亏170亿
  • 和幼儿做网站百度资源
  • 泸州网站制作兰州seo网站建设
  • 支持企业网站发布要怎么做餐饮营销策划与运营
  • 找个做游戏的视频网站好网站优化排名优化
  • 重庆网站开发商城网店培训班
  • 福田网站建设seo信科网络推广的主要工作内容
  • 漂亮网站底部代码怎么优化关键词排名优化
  • 句容工程建设招标网站最新国际军事动态
  • 温岭网站制作百度站长链接提交
  • 网站制作软件叫什么微商软文推广平台
  • 网站图片切换怎么做的99个创意营销方案
  • 制作应用的网站上海网站建设开发公司
  • 沈阳网站推广¥做下拉去118cr制作自己的网站
  • 建行个人网上银行上海网络优化服务