{servlet-name} ==> /WEB-INF/{servlet-name}-servlet.xml
web.xml 文件中:
my-dispatcher-name
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-myconfig.xml
1
应用程序的流程:
选择 archetype "maven-archetype-webapp"。
这样将创建项目,结构如下图所示:
4.0.0
com.yiibai
HelloSpringMVC
war
0.0.1-SNAPSHOT
HelloSpringMVC Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-core
4.1.4.RELEASE
org.springframework
spring-web
4.1.4.RELEASE
org.springframework
spring-webmvc
4.1.4.RELEASE
HelloSpringMVC
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
配置
web.xml:
HelloWorldSpring
spring-mvc
org.springframework.web.servlet.DispatcherServlet
1
spring-mvc
/
contextConfigLocation
/WEB-INF/root-context.xml
org.springframework.web.context.ContextLoaderListener
spring-mvc-servlet.xml
/WEB-INF/pages/
.jsp
注:
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/root-context.xml,
/WEB-INF/abc.xml
/WEB-INF/root-context.xml
HelloWorldController.java
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";
}
}
helloworld.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Spring4 MVC -HelloWorld
${greeting}
输入:
点击Run:
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";
}
......
}
7.2- 控制器中的重定向
使用前缀 "redirect:" ,该方法返回字符串,可以重定向到另一页面。参见图:
RedirectExampleController.java
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";
}
}
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", "[email protected]");
} else{
model.addAttribute("email", "Not set");
}
return "userInfo";
}
}
/WEB-INF/pages/userInfo.jsp
User Info
${name}
Email: ${email}
下面的代码片段显示了用法:
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";
}
}
/WEB-INF/pages/documentView.jsp
${documentName}
${documentName}
Site Prefix: ${sitePrefix}
Language: ${language}
ID: ${id}
Natural Text: ${naturalText}
注:方法不一定需要返回字符串类型。
使用@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";
}
}