在FileUpload一文中,我们初步了解了SpringMVC中View的用法,在例子中,通过给Model添加一个属性(model.addAttribute()),View对应的JSP就可以获取该值。本文再介绍一些View对应JSP取值的方式。
增加一个Controller,ViewsController:
- package org.springframework.samples.mvc.views;
- import javax.validation.Valid;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- @Controller
- @RequestMapping("/views/*")
- public class ViewsController {
- @RequestMapping(value="html", method=RequestMethod.GET)
- public String prepare(Model model) {
- model.addAttribute("foo", "bar");
- model.addAttribute("fruit", "apple");
- return "views/html";
- }
- @RequestMapping(value="/viewName", method=RequestMethod.GET)
- public void usingRequestToViewNameTranslator(Model model) {
- model.addAttribute("foo", "bar");
- model.addAttribute("fruit", "apple");
- }
- @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
- public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
- // No need to add @PathVariables "foo" and "fruit" to the model
- // They will be merged in the model before rendering
- return "views/html";
- }
- @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)
- public String dataBinding(@Valid JavaBean javaBean, Model model) {
- // JavaBean "foo" and "fruit" properties populated from URI variables
- return "views/dataBinding";
- }
- }
package org.springframework.samples.mvc.views; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/views/*") public class ViewsController { @RequestMapping(value="html", method=RequestMethod.GET) public String prepare(Model model) { model.addAttribute("foo", "bar"); model.addAttribute("fruit", "apple"); return "views/html"; } @RequestMapping(value="/viewName", method=RequestMethod.GET) public void usingRequestToViewNameTranslator(Model model) { model.addAttribute("foo", "bar"); model.addAttribute("fruit", "apple"); } @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET) public String pathVars(@PathVariable String foo, @PathVariable String fruit) { // No need to add @PathVariables "foo" and "fruit" to the model // They will be merged in the model before rendering return "views/html"; } @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET) public String dataBinding(@Valid JavaBean javaBean, Model model) { // JavaBean "foo" and "fruit" properties populated from URI variables return "views/dataBinding"; } }
1. 访问"http://localhost:8080/web/views/html",返回到"webapp/WEB-INF/views/views/html.jsp":
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ page session="false" %>
- <html>
- <head>
- <title>My HTML View</title>
- <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <div class="success">
- <h3>foo: "${foo}"</h3>
- <h3>fruit: "${fruit}"</h3>
- </div>
- </body>
- </html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>My HTML View</title> <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" /> </head> <body> <div class="success"> <h3>foo: "${foo}"</h3> <h3>fruit: "${fruit}"</h3> </div> </body> </html>
prepare(Model model)同FileUpload一样,通过model.addAttribite("foo", "bar");,JSP的${foo}就能获得"bar"。
2. 访问"http://localhost:8080/web/views/viewName",返回到"webapp/WEB-INF/views/views/viewName.jsp",这个jsp文件和html.jsp一样。
usingRequestToViewNameTranslator(Model model)和prepare(Model model)稍有不同,该方法的返回值为void,SpringMVC认为返回值为void的View名字就是@RequestMapping中映射的完整路径,即"views/viewName"。
3. 访问"http://localhost:8080/web/views/pathVariables/bar/orange",返回到"webapp/WEB-INF/views/views/html.jsp"
pathVars方法多了@PathVariable注解,该注解解析URL路径,并赋值给带有@PathVaribale的变量,View对应的JSP可以直接读取到这个变量的值。
4. 访问"http://localhost:8080/web/views/dataBinding/bar/orange",返回到"webapp/WEB-INF/views/views/dataBinding.jsp"
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ page session="false" %>
- <html>
- <head>
- <title>Data Binding with URI Template Variables</title>
- <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <div class="success">
- <h3>javaBean.foo: ${javaBean.foo}</h3>
- <h3>javaBean.fruit: ${javaBean.fruit}</h3>
- </div>
- </body>
- </html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>Data Binding with URI Template Variables</title> <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" /> </head> <body> <div class="success"> <h3>javaBean.foo: ${javaBean.foo}</h3> <h3>javaBean.fruit: ${javaBean.fruit}</h3> </div> </body> </html>
dataBinding(@Valid JavaBean javaBean, Model model)的方法参数中有个自定义的Java类,SpringMVC会自动解析URL路径,并且感知foo和fruit是否为JavaBean的属性,如果是,则将它赋值给JavaBean,非常智能。JSP现在得到的就是一个JavaBean,因此需要使用${javaBean.foo}获取具体值。
=================================================================================
=================================================================================
本节介绍SpringMVC中的表单,demo演示访问一个表单提交页面,填写表单的内容后使用jQuery的Ajax提交表单,将返回的文本信息显示出来。
记得在Struts1中有个FormBean的东西封装表单内容,在SpringMVC中也有,只不过SpringMVC更松耦合,只要写一个POJO就可以了,而不需要继承框架关联的类,看一下这个FormBean(只列出了主要属性):
- public class FormBean {
- @NotEmpty
- private String name;
- @Min(21)
- private int age;
- @DateTimeFormat(iso=ISO.DATE)
- @Past
- private Date birthDate;
- @MaskFormat("(###) ###-####")
- private String phone;
- @NumberFormat(pattern="$###,###.00")
- private BigDecimal currency;
- @NumberFormat(style=Style.PERCENT)
- private BigDecimal percent;
- private InquiryType inquiry;
- private String inquiryDetails;
- private boolean subscribeNewsletter;
- private Map<String, String> additionalInfo;
- ...
- }
public class FormBean { @NotEmpty private String name; @Min(21) private int age; @DateTimeFormat(iso=ISO.DATE) @Past private Date birthDate; @MaskFormat("(###) ###-####") private String phone; @NumberFormat(pattern="$###,###.00") private BigDecimal currency; @NumberFormat(style=Style.PERCENT) private BigDecimal percent; private InquiryType inquiry; private String inquiryDetails; private boolean subscribeNewsletter; private Map<String, String> additionalInfo; ... }
需要一个Controller,FormController:
- package org.springframework.samples.mvc.form;
- import javax.validation.Valid;
- import org.springframework.mvc.extensions.ajax.AjaxUtils;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.SessionAttributes;
- import org.springframework.web.context.request.WebRequest;
- import org.springframework.web.servlet.mvc.support.RedirectAttributes;
- @Controller
- @RequestMapping("/form")
- @SessionAttributes("formBean")
- public class FormController {
- // Invoked on every request
- @ModelAttribute
- public void ajaxAttribute(WebRequest request, Model model) {
- model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
- }
- // Invoked initially to create the "form" attribute
- // Once created the "form" attribute comes from the HTTP session (see @SessionAttributes)
- @ModelAttribute("formBean")
- public FormBean createFormBean() {
- return new FormBean();
- }
- @RequestMapping(method=RequestMethod.GET)
- public void form() {
- }
- @RequestMapping(method=RequestMethod.POST)
- public String processSubmit(@Valid FormBean formBean, BindingResult result,
- @ModelAttribute("ajaxRequest") boolean ajaxRequest,
- Model model, RedirectAttributes redirectAttrs) {
- if (result.hasErrors()) {
- return null;
- }
- // Typically you would save to a db and clear the "form" attribute from the session
- // via SessionStatus.setCompleted(). For the demo we leave it in the session.
- String message = "Form submitted successfully. Bound " + formBean;
- // Success response handling
- if (ajaxRequest) {
- // prepare model for rendering success message in this request
- model.addAttribute("message", message);
- return null;
- } else {
- // store a success message for rendering on the next request after redirect
- // redirect back to the form to render the success message along with newly bound values
- redirectAttrs.addFlashAttribute("message", message);
- return "redirect:/form";
- }
- }
- }
package org.springframework.samples.mvc.form; import javax.validation.Valid; import org.springframework.mvc.extensions.ajax.AjaxUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller @RequestMapping("/form") @SessionAttributes("formBean") public class FormController { // Invoked on every request @ModelAttribute public void ajaxAttribute(WebRequest request, Model model) { model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request)); } // Invoked initially to create the "form" attribute // Once created the "form" attribute comes from the HTTP session (see @SessionAttributes) @ModelAttribute("formBean") public FormBean createFormBean() { return new FormBean(); } @RequestMapping(method=RequestMethod.GET) public void form() { } @RequestMapping(method=RequestMethod.POST) public String processSubmit(@Valid FormBean formBean, BindingResult result, @ModelAttribute("ajaxRequest") boolean ajaxRequest, Model model, RedirectAttributes redirectAttrs) { if (result.hasErrors()) { return null; } // Typically you would save to a db and clear the "form" attribute from the session // via SessionStatus.setCompleted(). For the demo we leave it in the session. String message = "Form submitted successfully. Bound " + formBean; // Success response handling if (ajaxRequest) { // prepare model for rendering success message in this request model.addAttribute("message", message); return null; } else { // store a success message for rendering on the next request after redirect // redirect back to the form to render the success message along with newly bound values redirectAttrs.addFlashAttribute("message", message); return "redirect:/form"; } } }
FormController只有一个@RequestMapping,通过GET,POST来区分是访问表单页面(GET),还是提交表单(POST)。在这个类中有:
- @ModelAttribute("formBean")
- public FormBean createFormBean() {
- return new FormBean();
- }
@ModelAttribute("formBean") public FormBean createFormBean() { return new FormBean(); }
它表示访问"http://localhost:8080/web/form/"时,就初始化一个叫formBean名字的属性放在Model中。在类的开头有:
- @SessionAttributes("formBean")
@SessionAttributes("formBean")
它表示Model中的这个formBean在session的范围内有效,想一下,session是跟浏览器窗口关联的,窗口关闭,session就失效,所以每次打开一个新的表单页面都会生成一个新的formBean,另外填写完表单内容以POST方式提交后,formBean会根据提交的参数自动设置FormBean的值。
- @ModelAttribute
- public void ajaxAttribute(WebRequest request, Model model) {
- model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
- }
@ModelAttribute public void ajaxAttribute(WebRequest request, Model model) { model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request)); }
该配置会导致每次访问"http://localhost:8080/web/form/",都会设置一个ajaxRequest值(因为没有象formBean一样设置ajaxRequest的范围,默认的范围为Request级,每次请求都执行)。之前说过填好表单后通过jQuery的Ajax提交表单,通过这个配置,就知道请求是否是Ajax请求。
- @RequestMapping(method=RequestMethod.GET)
- public void form() {
- }
@RequestMapping(method=RequestMethod.GET) public void form() { }
访问"http://localhost:8080/web/form/",返回到"webapp/WEB-INF/views/form.jsp"。
- @RequestMapping(method=RequestMethod.POST)
- public String processSubmit(@Valid FormBean formBean, BindingResult result,
- @ModelAttribute("ajaxRequest") boolean ajaxRequest,
- Model model, RedirectAttributes redirectAttrs) {
- if (result.hasErrors()) {
- return null;
- }
- // Typically you would save to a db and clear the "form" attribute from the session
- // via SessionStatus.setCompleted(). For the demo we leave it in the session.
- String message = "Form submitted successfully. Bound " + formBean;
- // Success response handling
- if (ajaxRequest) {
- // prepare model for rendering success message in this request
- model.addAttribute("message", message);
- return null;
- } else {
- // store a success message for rendering on the next request after redirect
- // redirect back to the form to render the success message along with newly bound values
- redirectAttrs.addFlashAttribute("message", message);
- return "redirect:/form";
- }
- }
@RequestMapping(method=RequestMethod.POST) public String processSubmit(@Valid FormBean formBean, BindingResult result, @ModelAttribute("ajaxRequest") boolean ajaxRequest, Model model, RedirectAttributes redirectAttrs) { if (result.hasErrors()) { return null; } // Typically you would save to a db and clear the "form" attribute from the session // via SessionStatus.setCompleted(). For the demo we leave it in the session. String message = "Form submitted successfully. Bound " + formBean; // Success response handling if (ajaxRequest) { // prepare model for rendering success message in this request model.addAttribute("message", message); return null; } else { // store a success message for rendering on the next request after redirect // redirect back to the form to render the success message along with newly bound values redirectAttrs.addFlashAttribute("message", message); return "redirect:/form"; } }
processSubmit第一个参数formBean封装了页面提交的表单参数,注意到它前面有一个@Valid,因此有第二个参数BindingResult result, 该参数可以知道表单是否验证通过。第三个参数获取Model中的属性值"ajaxRequest",在方法体中,判断ajaxRequest,如果是,将返回"webapp/WEB-INF/views/form.jsp",如果不是,将它重定向到一个jsp,当然,这个条件在本例子中没有使用到。
最后看看非常复杂的form.jsp,该jsp使用了大量的SpringMVC的标签:
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
- <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
- <%@ page session="false" %>
- <c:if test="${!ajaxRequest}">
- <html>
- <head>
- <title>forms | mvc-showcase</title>
- <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" />
- <script type="text/javascript" src="<c:url value="/resources/jquery/1.6/jquery.js" />"></script>
- </head>
- <body>
- </c:if>
- <div id="formsContent">
- <h2>Forms</h2>
- <p>
- See the <code>org.springframework.samples.mvc.form</code> package for the @Controller code
- </p>
- <form:form id="form" method="post" modelAttribute="formBean" cssClass="cleanform">
- <div class="header">
- <h2>Form</h2>
- <c:if test="${not empty message}">
- <div id="message" class="success">${message}</div>
- </c:if>
- <s:bind path="*">
- <c:if test="${status.error}">
- <div id="message" class="error">Form has errors</div>
- </c:if>
- </s:bind>
- </div>
- <fieldset>
- <legend>Personal Info</legend>
- <form:label path="name">
- Name <form:errors path="name" cssClass="error" />
- </form:label>
- <form:input path="name" />
- <form:label path="age">
- Age <form:errors path="age" cssClass="error" />
- </form:label>
- <form:input path="age" />
- <form:label path="birthDate">
- Birth Date (in form yyyy-mm-dd) <form:errors path="birthDate" cssClass="error" />
- </form:label>
- <form:input path="birthDate" />
- <form:label path="phone">
- Phone (in form (###) ###-####) <form:errors path="phone" cssClass="error" />
- </form:label>
- <form:input path="phone" />
- <form:label path="currency">
- Currency (in form $#.##) <form:errors path="currency" cssClass="error" />
- </form:label>
- <form:input path="currency" />
- <form:label path="percent">
- Percentage (in form ##%) <form:errors path="percent" cssClass="error" />
- </form:label>
- <form:input path="percent" />
- </fieldset>
- <fieldset>
- <legend>Inquiry</legend>
- <form:label path="inquiry">
- Type (select one)
- </form:label>
- <form:select path="inquiry">
- <form:option value="comment">Comment</form:option>
- <form:option value="feedback">Feedback</form:option>
- <form:option value="suggestion">Suggestion</form:option>
- </form:select>
- <form:label path="inquiryDetails">
- Details
- </form:label>
- <form:textarea path="inquiryDetails" />
- </fieldset>
- <fieldset class="checkbox">
- <legend>Request Additional Info</legend>
- <label><form:checkbox path="additionalInfo[mvc]" value="true" />on Spring MVC</label>
- <label><form:checkbox path="additionalInfo[java]" value="true" />on Java (4-ever)</label>
- </fieldset>
- <fieldset class="radio">
- <legend>Subscribe to Newsletter?</legend>
- <label><form:radiobutton path="subscribeNewsletter" value="true" />Yes</label>
- <label><form:radiobutton path="subscribeNewsletter" value="false" /> No</label>
- </fieldset>
- <p><button type="submit">Submit</button></p>
- </form:form>
- <script type="text/javascript">
- $(document).ready(function() {
- $("#form").submit(function() {
- $.post($(this).attr("action"), $(this).serialize(), function(html) {
- $("#formsContent").replaceWith(html);
- $('html, body').animate({ scrollTop: $("#message").offset().top }, 500);
- });
- return false;
- });
- });
- </script>
- </div>
- <c:if test="${!ajaxRequest}">
- </body>
- </html>
- </c:if>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> <%@ page session="false" %> <c:if test="${!ajaxRequest}"> <html> <head> <title>forms | mvc-showcase</title> <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="<c:url value="/resources/jquery/1.6/jquery.js" />"></script> </head> <body> </c:if> <div id="formsContent"> <h2>Forms</h2> <p> See the <code>org.springframework.samples.mvc.form</code> package for the @Controller code </p> <form:form id="form" method="post" modelAttribute="formBean" cssClass="cleanform"> <div class="header"> <h2>Form</h2> <c:if test="${not empty message}"> <div id="message" class="success">${message}</div> </c:if> <s:bind path="*"> <c:if test="${status.error}"> <div id="message" class="error">Form has errors</div> </c:if> </s:bind> </div> <fieldset> <legend>Personal Info</legend> <form:label path="name"> Name <form:errors path="name" cssClass="error" /> </form:label> <form:input path="name" /> <form:label path="age"> Age <form:errors path="age" cssClass="error" /> </form:label> <form:input path="age" /> <form:label path="birthDate"> Birth Date (in form yyyy-mm-dd) <form:errors path="birthDate" cssClass="error" /> </form:label> <form:input path="birthDate" /> <form:label path="phone"> Phone (in form (###) ###-####) <form:errors path="phone" cssClass="error" /> </form:label> <form:input path="phone" /> <form:label path="currency"> Currency (in form $#.##) <form:errors path="currency" cssClass="error" /> </form:label> <form:input path="currency" /> <form:label path="percent"> Percentage (in form ##%) <form:errors path="percent" cssClass="error" /> </form:label> <form:input path="percent" /> </fieldset> <fieldset> <legend>Inquiry</legend> <form:label path="inquiry"> Type (select one) </form:label> <form:select path="inquiry"> <form:option value="comment">Comment</form:option> <form:option value="feedback">Feedback</form:option> <form:option value="suggestion">Suggestion</form:option> </form:select> <form:label path="inquiryDetails"> Details </form:label> <form:textarea path="inquiryDetails" /> </fieldset> <fieldset class="checkbox"> <legend>Request Additional Info</legend> <label><form:checkbox path="additionalInfo[mvc]" value="true" />on Spring MVC</label> <label><form:checkbox path="additionalInfo[java]" value="true" />on Java (4-ever)</label> </fieldset> <fieldset class="radio"> <legend>Subscribe to Newsletter?</legend> <label><form:radiobutton path="subscribeNewsletter" value="true" />Yes</label> <label><form:radiobutton path="subscribeNewsletter" value="false" /> No</label> </fieldset> <p><button type="submit">Submit</button></p> </form:form> <script type="text/javascript"> $(document).ready(function() { $("#form").submit(function() { $.post($(this).attr("action"), $(this).serialize(), function(html) { $("#formsContent").replaceWith(html); $('html, body').animate({ scrollTop: $("#message").offset().top }, 500); }); return false; }); }); </script> </div> <c:if test="${!ajaxRequest}"> </body> </html> </c:if>
相关推荐
- **CommonsMultipartResolver**:Spring MVC通过集成Apache Commons FileUpload库,提供了处理multipart/form-data类型请求的能力。CommonsMultipartFile对象可以用来封装上传的文件。 - **@RequestParam**:在...
Spring的Web MVC架构模式是一种在Java开发中广泛使用的轻量级框架,用于构建高效、可测试和可维护的Web应用程序。该框架的核心是DispatcherServlet,它作为中央调度器,负责接收HTTP请求并将其转发给合适的处理器。...
Spring MVC 是 Spring 框架的一部分,主要用于构建基于 Model-View-Controller (MVC) 设计模式的 Web 应用程序。它提供了丰富的功能来简化 Web 开发过程,包括请求映射、视图解析、表单绑定等。本文将详细介绍 ...
Spring MVC 和 ExtJS 是两种广泛应用于开发Web应用程序的技术。Spring MVC是Spring框架的一部分,它提供了一个模型-视图-控制器架构来构建可维护且松耦合的Java Web应用。而ExtJS是一个强大的JavaScript库,主要用于...
在本文中,我们将探讨如何在NetBeans IDE中创建一个基于Spring Web MVC的简单应用程序。...对于初学者来说,这是一个很好的起点,可以帮助理解Spring MVC的工作原理,并逐步深入到更复杂的应用开发。
Spring MVC programming model essentials, Spring MVC views and form processing, Spring Web Flow essentials, and Spring Web Flow actions and configuration. The Pivotal Certified Spring Web Application ...
Spring MVC 是一个基于Java的轻量级Web应用框架,它为构建RESTful应用程序提供了强大的支持。这个"spring mvc学习代码"的压缩包文件显然包含了使用Spring MVC与Hibernate集成开发Web应用的相关示例和实践。下面我们...
在本文中,我们将深入探讨如何使用Spring MVC 4.0框架与Spring Security 3.2进行集成,构建一个基础的安全管理应用。同时,我们还将引入MyBatis作为持久层框架,来处理数据库交互。让我们逐步了解这个过程。 首先,...
Spring MVC 是一个基于 Java 的轻量级 Web 开发框架,它是 Spring 框架的一部分,主要用于构建 MVC(Model-View-Controller)模式的 Web 应用程序。在本示例中,我们将深入探讨如何利用 Spring MVC 实现用户登录功能...
Spring MVC作为Spring框架的重要组成部分之一,在Spring 2.5版本中引入了基于注解的配置方式,这使得开发者能够更便捷地构建Web应用程序。相比于传统的基于XML的配置方式,基于注解的配置更加简洁、直观,同时也提高...
Spring MVC 是 Spring 框架的一个模块,它为构建基于 Java 的 Web 应用程序提供了一个模型-视图-控制器(MVC)架构。在本文中,我们将深入探讨如何使用 Maven 搭建一个包含 Spring MVC、MyBatis 和 Velocity 的基本...
`web.xml`文件是Web应用程序的核心配置文件,用于初始化Spring MVC的DispatcherServlet。 ```xml <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi=...
在本教程中,我们将深入探讨如何搭建一个基于Spring框架的Web入门级项目,重点是使用Spring MVC和Spring JDBC实现简单的用户登录功能。Spring是一个广泛使用的Java企业级应用框架,而Spring MVC是它的一个核心模块,...
Spring Web MVC是Spring框架的一个核心模块,主要用于构建基于Servlet容器的Web应用程序。它提供了一个灵活、强大的机制来处理HTTP请求,并将其映射到适当的控制器上执行业务逻辑。Spring Web MVC的设计理念之一就是...
Spring MVC 是 Spring Framework 的一部分,专门用于构建基于 Web 的应用程序。Spring MVC 3.0 版本在继承了之前版本诸多优点的基础上,进一步增强了对注解的支持,并且加强了对 RESTful 风格的支持。 #### 入门...
Spring MVC是Java开发Web应用程序的一个强大工具,它提供了一种优雅的方式来处理HTTP请求,特别是表单数据的提交。 首先,`pom.xml`文件是Maven项目对象模型的配置文件,用于管理项目的依赖关系。在Spring MVC项目...
5.4.1. Writing a form-handling controller 5.4.2. Validating forms 5.5. Summary Chapter 6. Rendering web views 6.1. Understanding view resolution 6.2. Creating JSP views 6.2.1. Configuring a JSP-ready ...
3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..............................................
<property name="templateLoaderPath" value="/WEB-INF/views/"/> <prop key="default_encoding">UTF-8 <prop key="number_format">0.#### <entry key="date_format" value="yyyy-MM-dd HH:mm:ss"/>...