如何通过Spring MVC实现高效的企业级Web应用开发?

2026-04-15 09:2010阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计720个文字,预计阅读时间需要3分钟。

如何通过Spring MVC实现高效的企业级Web应用开发?

Spring MVC的全面使用Spring MVC是Spring框架的一个子产品,它是一种典型的MVC(Model-View-Controller)架构风格的实现。与Struts等框架不同,Spring MVC不是简单的变种,也不是不完整地基于MVC系统的框架。对于初学者或希望了解MVC的人来说,Spring MVC是一个很好的选择。

spring mvc的具体使用

Spring MVC 它是Spring框架的一个子产品,它是一个典型的教科书式的mvc构架,而不像struts等都是变种或者不是完全基于MVC系统的框架,对于初学者或者想了解MVC的人来说我觉得 Spring是最好的,它的实现就是教科书, PS:spring mvc 希望用户在使用controller不再调用任何servlet里面的对象 -->解耦方式(耦:耦合) 1.导入架包 -->直接导入Spring的所有jar即可(因属于Spring的子产品,后面也会用到Spring) -->除Spring的架包,还需要导入commons-logging-1.1.1和标准标签库的包 2.添加Web.xml中关于SpringMVC的配置 -->建议复制标准配置,然后修改即可 spring org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:/spring_servlet.xml 1 spring / default *.js 3.在src下添加springmvc.xml的配置 -->建议复制标准配置,然后修改即可

text/html;charset=UTF-8 4.创建普通类,添加控制器@Controller注解,并在方法添加@RequestMapping注解即可 package org.jxnd.controller; import javax.validation.Valid; import org.jxnd.bean.User; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; @Controller @SessionAttributes(value={"aaa","info"}) public class UserController { @RequestMapping(value = "/dd") public String dd() { return "default"; } @RequestMapping(value = "/test1/{name}/{id}") public String test1(@PathVariable("name") String name, @PathVariable("id") String id) { System.out.println("name:" + name + "id:" + id); return "index"; } @RequestMapping(value = "/test2") public String test2(@RequestParam(value = "cba", required = false, defaultValue = "000") String id) { System.out.println("id: " + id); return "index"; } @RequestMapping(value = "/test3") public String test3(User user) { System.out.println(user); return "index"; } @RequestMapping(value = "/ajax", produces = "text/plain;charset=utf-8") @ResponseBody public String ajax(String name, String password) { System.out.println("name:" + name + " password:" + password); return "我是ajax返回的信息:" + name + "密码:" + password; } @ModelAttribute("info") public User getUser() { return new User(); } @RequestMapping(value = "/test4") public String test4(@Valid @ModelAttribute("info") User info, BindingResult br) { System.out.println(br.getErrorCount()); for (ObjectError oe : br.getAllErrors()) { System.out.println(oe.getDefaultMessage()); } return "default"; } }

如何通过Spring MVC实现高效的企业级Web应用开发?
标签:

本文共计720个文字,预计阅读时间需要3分钟。

如何通过Spring MVC实现高效的企业级Web应用开发?

Spring MVC的全面使用Spring MVC是Spring框架的一个子产品,它是一种典型的MVC(Model-View-Controller)架构风格的实现。与Struts等框架不同,Spring MVC不是简单的变种,也不是不完整地基于MVC系统的框架。对于初学者或希望了解MVC的人来说,Spring MVC是一个很好的选择。

spring mvc的具体使用

Spring MVC 它是Spring框架的一个子产品,它是一个典型的教科书式的mvc构架,而不像struts等都是变种或者不是完全基于MVC系统的框架,对于初学者或者想了解MVC的人来说我觉得 Spring是最好的,它的实现就是教科书, PS:spring mvc 希望用户在使用controller不再调用任何servlet里面的对象 -->解耦方式(耦:耦合) 1.导入架包 -->直接导入Spring的所有jar即可(因属于Spring的子产品,后面也会用到Spring) -->除Spring的架包,还需要导入commons-logging-1.1.1和标准标签库的包 2.添加Web.xml中关于SpringMVC的配置 -->建议复制标准配置,然后修改即可 spring org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:/spring_servlet.xml 1 spring / default *.js 3.在src下添加springmvc.xml的配置 -->建议复制标准配置,然后修改即可

text/html;charset=UTF-8 4.创建普通类,添加控制器@Controller注解,并在方法添加@RequestMapping注解即可 package org.jxnd.controller; import javax.validation.Valid; import org.jxnd.bean.User; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; @Controller @SessionAttributes(value={"aaa","info"}) public class UserController { @RequestMapping(value = "/dd") public String dd() { return "default"; } @RequestMapping(value = "/test1/{name}/{id}") public String test1(@PathVariable("name") String name, @PathVariable("id") String id) { System.out.println("name:" + name + "id:" + id); return "index"; } @RequestMapping(value = "/test2") public String test2(@RequestParam(value = "cba", required = false, defaultValue = "000") String id) { System.out.println("id: " + id); return "index"; } @RequestMapping(value = "/test3") public String test3(User user) { System.out.println(user); return "index"; } @RequestMapping(value = "/ajax", produces = "text/plain;charset=utf-8") @ResponseBody public String ajax(String name, String password) { System.out.println("name:" + name + " password:" + password); return "我是ajax返回的信息:" + name + "密码:" + password; } @ModelAttribute("info") public User getUser() { return new User(); } @RequestMapping(value = "/test4") public String test4(@Valid @ModelAttribute("info") User info, BindingResult br) { System.out.println(br.getErrorCount()); for (ObjectError oe : br.getAllErrors()) { System.out.println(oe.getDefaultMessage()); } return "default"; } }

如何通过Spring MVC实现高效的企业级Web应用开发?
标签: