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

常州市做网站的公司网站设计论文提纲

常州市做网站的公司,网站设计论文提纲,天津网站建设首选津坤科技,网站整体色调Spring_MVC 高级特性详解与实战应用 一、数据绑定与表单处理 在 Web 开发中,数据绑定和表单处理是常见的需求。Spring_MVC 提供了强大的数据绑定功能,可以方便地将请求参数绑定到业务对象。 1. 使用 ModelAttribute 绑定请求参数到对象 ModelAttribu…

Spring_MVC 高级特性详解与实战应用

一、数据绑定与表单处理

在 Web 开发中,数据绑定和表单处理是常见的需求。Spring_MVC 提供了强大的数据绑定功能,可以方便地将请求参数绑定到业务对象。

1. 使用 @ModelAttribute 绑定请求参数到对象

@ModelAttribute 注解用于将请求参数绑定到业务对象的属性上。控制器方法的参数可以是一个业务对象,Spring_MVC 会自动将请求参数绑定到该对象的属性。

@Controller
@RequestMapping("/user")
public class UserFormController {@RequestMapping(value = "/form", method = RequestMethod.GET)public String showForm(Model model) {model.addAttribute("user", new User());return "userForm";}@RequestMapping(value = "/submit", method = RequestMethod.POST)public String submitForm(@ModelAttribute User user) {System.out.println("User submitted: " + user.getName());return "success";}
}
  • showForm 方法用于显示表单页面,并向视图传递一个空的 User 对象。
  • submitForm 方法用于处理表单提交,@ModelAttribute 将请求参数绑定到 User 对象。

2. 使用 @InitBinder 自定义数据绑定逻辑

对于一些特殊的类型转换需求,可以使用 @InitBinder 注解来自定义数据绑定逻辑。通过注册自定义的 PropertyEditorConverter,可以处理日期、枚举等类型的转换。

@Controller
@RequestMapping("/user")
public class CustomBindingController {@InitBinderpublic void initBinder(WebDataBinder binder) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}@RequestMapping(value = "/submitWithDate", method = RequestMethod.POST)public String handleUserWithDate(@ModelAttribute User user) {System.out.println("User birth date: " + user.getBirthDate());return "success";}
}
  • initBinder 方法用于注册自定义的日期编辑器。
  • handleUserWithDate 方法处理带有日期字段的用户提交。

二、异常处理

在 Web 应用开发中,异常处理是保证应用稳定性和用户体验的重要环节。Spring_MVC 提供了多种异常处理机制,可以方便地处理各种异常情况。

1. 使用 @ControllerAdvice@ExceptionHandler 全局处理异常

@ControllerAdvice 注解用于定义一个全局异常处理器,@ExceptionHandler 注解用于指定处理特定异常的方法。

@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(ResourceNotFoundException.class)public String handleResourceNotFound(ResourceNotFoundException ex, HttpServletRequest request) {request.setAttribute("errMsg", ex.getMessage());return "error/404";}@ExceptionHandler(Exception.class)public String handleGeneralException(Exception ex, HttpServletRequest request) {request.setAttribute("errMsg", "系统发生错误,请联系管理员");return "error/500";}
}
  • handleResourceNotFound 方法处理 ResourceNotFoundException 异常,并返回 404 页面。
  • handleGeneralException 方法处理其他异常,并返回 500 页面。

2. 使用 @ResponseStatus 指定响应状态码

@ResponseStatus 注解用于指定异常处理方法的 HTTP 响应状态码。

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Resource not found")
public class ResourceNotFoundException extends RuntimeException {public ResourceNotFoundException(String message) {super(message);}
}
  • 当抛出 ResourceNotFoundException 异常时,响应状态码为 404,原因短语为 “Resource not found”。

三、文件上传与下载

文件上传和下载是 Web 应用中常见的功能。Spring_MVC 提供了简单易用的 API 来处理文件上传和下载。

1. 文件上传

@Controller
@RequestMapping("/file")
public class FileUploadController {@RequestMapping(value = "/upload", method = RequestMethod.POST)public String handleFileUpload(@RequestParam("file") MultipartFile file) {if (!file.isEmpty()) {try {byte[] bytes = file.getBytes();// 处理文件保存逻辑,例如保存到服务器文件系统或数据库System.out.println("File uploaded successfully: " + file.getOriginalFilename());return "uploadSuccess";} catch (IOException e) {e.printStackTrace();}}return "uploadFailure";}
}
  • @RequestParam("file") 用于接收上传的文件。
  • MultipartFile 表示上传的文件对象,可以通过其方法获取文件内容、文件名等信息。

2. 文件下载

@Controller
@RequestMapping("/file")
public class FileDownloadController {@RequestMapping(value = "/download", method = RequestMethod.GET)public ResponseEntity<Resource> downloadFile() {FileSystemResource file = new FileSystemResource("path/to/your/file.txt");return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.txt").body(file);}
}
  • ResponseEntity 用于构建 HTTP 响应。
  • Resource 表示资源文件,可以是文件系统中的文件或其他类型的资源。
  • 通过设置响应头 Content-Dispositionattachment,可以提示浏览器下载文件。

四、拦截器(Interceptors)

拦截器是 Spring_MVC 提供的一种 AOP(面向切面编程)机制,可以用于在请求处理的各个阶段执行自定义逻辑,例如日志记录、权限验证等。

1. 创建自定义拦截器

public class LogInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("Request processing started: " + request.getRequestURI());return true; // 返回 true 继续处理请求,返回 false 中断请求处理}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("Request processing finished: " + request.getRequestURI());}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("Request completed: " + request.getRequestURI());}
}
  • preHandle 方法在请求处理之前执行,可以用于权限验证等逻辑。
  • postHandle 方法在请求处理完成后执行,但视图渲染之前,可以用于修改模型数据等。
  • afterCompletion 方法在请求处理完成之后执行,可以用于资源清理等操作。

2. 配置拦截器

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LogInterceptor()).addPathPatterns("/user/*") // 拦截 /user/ 开头的请求.excludePathPatterns("/user/login"); // 排除 /user/login 请求}
}
  • addInterceptors 方法用于添加自定义拦截器。
  • addPathPatterns 方法用于指定拦截器拦截的请求路径。
  • excludePathPatterns 方法用于指定拦截器不拦截的请求路径。

五、国际化(I18N)

国际化是开发多语言应用的重要功能。Spring_MVC 支持国际化,可以方便地实现多语言切换。

1. 创建消息资源文件

创建不同语言的消息资源文件,例如:

  • messages_en.properties

    greeting=Hello, {0}!
    welcome=Welcome to our website.
    
  • messages_zh.properties

    greeting=你好,{0}!
    welcome=欢迎来到我们的网站。
    

2. 配置消息源

@Configuration
public class I18nConfig {@Beanpublic LocaleResolver localeResolver() {SessionLocaleResolver resolver = new SessionLocaleResolver();resolver.setDefaultLocale(Locale.US); // 默认语言为英文return resolver;}@Beanpublic MessageSource messageSource() {ResourceBundleMessageSource source = new ResourceBundleMessageSource();source.setBasename("messages"); // 消息资源文件的 basenamesource.setDefaultEncoding("UTF-8");return source;}
}
  • LocaleResolver 用于解析客户端的语言环境。
  • MessageSource 用于加载消息资源文件。

3. 使用国际化消息

@Controller
@RequestMapping("/i18n")
public class I18nController {@Autowiredprivate MessageSource messageSource;@RequestMapping(value = "/greeting", method = RequestMethod.GET)public String showGreeting(Model model, Locale locale) {String greeting = messageSource.getMessage("greeting", new Object[]{"World"}, locale);model.addAttribute("greeting", greeting);return "greetingPage";}
}
  • MessageSource.getMessage 方法用于获取国际化消息。
  • Locale 参数用于指定当前的语言环境。
<!-- greetingPage.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Greeting</title>
</head>
<body><h1>${greeting}</h1><a href="?lang=zh">中文</a> | <a href="?lang=en">English</a>
</body>
</html>
  • 在页面中可以通过 ${greeting} 输出国际化消息。
  • 通过添加 lang 参数可以切换语言。

文章转载自:

http://67exKb19.cxLys.cn
http://SWQlyyv4.cxLys.cn
http://9LeMEVuD.cxLys.cn
http://2Ymq97jI.cxLys.cn
http://A0BN1iK7.cxLys.cn
http://lkfEOsYC.cxLys.cn
http://v7c386PT.cxLys.cn
http://arwJ7cfS.cxLys.cn
http://3WuJzQEp.cxLys.cn
http://TGC3iKZT.cxLys.cn
http://Sw31T2EK.cxLys.cn
http://JDMbodss.cxLys.cn
http://aTR1s8Ob.cxLys.cn
http://gl0QiU2z.cxLys.cn
http://vOVTovIK.cxLys.cn
http://uMJNNECG.cxLys.cn
http://gsqHSX1u.cxLys.cn
http://xmI8PIVr.cxLys.cn
http://evOekP9W.cxLys.cn
http://z9tbNRXY.cxLys.cn
http://i9SI2INi.cxLys.cn
http://EkV91qPN.cxLys.cn
http://m18n7Car.cxLys.cn
http://3984TwB2.cxLys.cn
http://38Ogzb9B.cxLys.cn
http://0irU87Vz.cxLys.cn
http://5IhpSdtb.cxLys.cn
http://f3Lb45q6.cxLys.cn
http://6EutR4iW.cxLys.cn
http://G8WYNPPA.cxLys.cn
http://www.dtcms.com/wzjs/773526.html

相关文章:

  • 佛山做网站制作公司手机h5建站
  • 手机网站生成工具深圳数字展厅
  • 渭南市住房和城乡建设局官方网站wordpress 活动模板
  • 网站推广策划方案3000字北京网站制作建设公司哪家好
  • 招标建设网站石家庄学设计的正规学校
  • 备案成功的网站可以更换域名吗广东网站建设模板
  • 微信小程序在哪里找到广州seo技术外包公司
  • 网站注册需要多少钱用友erp软件
  • 以色列网站后缀做网站是什么专业什么工作
  • 个人怎么做贷款网站创意名字设计
  • 宁夏固原建设网站要做网站到哪里做
  • 自己怎么做短视频网站淘宝关键词热度查询工具
  • 网站开发行业怎么样电子商务网站建设 市场分析
  • 微信公众平台视频网站开发wordpress 主题重置
  • 网站正在维护模板免费的行情网站下载安装
  • 垂直网站怎么做扬州市城市建设投资公司网站
  • 邢台企业网站建设报价云主机 小型网站
  • 网站的开发环境设计宣传片制作报价单
  • 百度秒收网站cms建站
  • 聊城那里有做网站青岛十大外贸公司
  • html5手机 网站漳浦县网站建设
  • 品牌网站建设預定大蝌蚪数码产品在哪里做网站
  • 微网站模板标签外包加工网是骗人的吗
  • 青岛做企业网站的公司网站制作怎么填
  • 电子政务网站建设的挑战wordpress 开发h5页面跳转
  • 泉州专业网站建设费用网页界面设计欣赏
  • 有哪些做搞笑视频的网站wordpress如何开启page页面评论
  • 用dw做的网站怎么上传wordpress 后台实现轮播图
  • 旅游网站项目计划书用vs2012怎么做网站
  • 公司网站管理属于什么职位网站空间200m