<servlet> <servlet-name>my-dispatcher-name</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- override default name {servlet-name}-servlet.xml --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-myconfig.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>应用程序的流程:
<projectxmlns="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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yiibai</groupId> <artifactId>HelloSpringMVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>HelloSpringMVC Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Servlet Library --> <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring dependencies --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>HelloSpringMVC</finalName> <plugins> <!-- Config: Maven Tomcat Plugin --> <!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <!-- Config: contextPath and Port (Default - /HelloSpringMVC : 8080) --> <!-- <configuration> <path>/</path> <port>8899</port> </configuration> --> </plugin> </plugins> </build> </project>配置 web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>HelloWorldSpring</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Other XML Configuration --> <!-- Load by Spring ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <!-- Spring ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>Spring MVC 的 DispatcherServlet将根据原则读取XML配置文件:
<?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <context:component-scan base-package="com.yiibai.tutorial.springmvc"/> <context:annotation-config/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>注:
<!-- web.xml --> <!-- Spring ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Load by Spring ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/root-context.xml, /WEB-INF/abc.xml </param-value> </context-param>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Empty --> </beans>
package com.yiibai.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping("/hello") public String hello(Model model) { model.addAttribute("greeting", "Hello Spring MVC"); return"helloworld"; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring4 MVC -HelloWorld</title> </head> <body> <h1>${greeting}</h1> </body> </html>
package com.yiibai.springmvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.RequestParam; @Controller public class OtherExampleController { ...... @RequestMapping("/somePath") public String requestResponseExample(HttpServletRequest request, HttpServletResponse reponses, Model model) { // Todo something here return "someView"; } ...... }
package com.yiibai.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class RedirectExampleController { @RequestMapping(value = "/redirect", method = RequestMethod.GET) public String authorInfo(Model model) { // Do somethong here return "redirect:/hello"; } }运行URL:
package com.yiibai.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class RequestParamExampleController { @RequestMapping("/user") public String userInfo(Model model, @RequestParam(value = "name", defaultValue = "Guest") String name) { model.addAttribute("name", name); if("admin".equals(name)) { model.addAttribute("email", "admin@yiibai.com"); } else{ model.addAttribute("email", "Not set"); } return "userInfo"; } }
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>User Info</title> </head> <body> <h2>${name}</h2> Email: ${email} <br> </body> </html>运行 URL:
package com.yiibai.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class PathVariableExampleController { /** * @PathVariable Example: * */ @RequestMapping("/web/fe/{sitePrefix}/{language}/document/{id}/{naturalText}") public String documentView(Model model, @PathVariable(value = "sitePrefix") String sitePrefix, @PathVariable(value = "language") String language, @PathVariable(value = "id") Long id, @PathVariable(value = "naturalText") String naturalText) { model.addAttribute("sitePrefix", sitePrefix); model.addAttribute("language", language); model.addAttribute("id", id); model.addAttribute("naturalText", naturalText); String documentName = "Java tutorial for Beginners"; if(id == 8108) { documentName = "Spring MVC for Beginners"; } model.addAttribute("documentName", documentName); return "documentView"; } }
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>${documentName}</title> </head> <body> <h3>${documentName}</h3> Site Prefix: ${sitePrefix} <br> Language: ${language} <br> ID: ${id} <br> Natural Text: ${naturalText} <br> </body> </html>运行 URL:
注:方法不一定需要返回字符串类型。
使用@ResponseBody和方法返回字符串的简单例子。
package com.yiibai.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ResponseBodyExample1Controller { // Simple example, method returns String. @RequestMapping(value = "/saveResult") @ResponseBody publicString authorInfo(Model model) { return "saved"; } }运行示例的结果:
Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有