标签:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Spring MVC配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> 默认 </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> </web-app>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven /> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> <context:component-scan base-package="com.xin" /> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 我们可以在其中添加我们所需要配置的bean,也可以添加相应的数据库连接和事务处理等等,方便后续拓展 --> </beans>
@Controller public class WelcomeController{ //访问下面方法地址:http://localhost:8080/Spring-mvc/login/wuxin @RequestMapping(value = "/login/{user}", method = RequestMethod.GET) public ModelAndView myMethod(HttpServletRequest request,HttpServletResponse response, @PathVariable("user") String user, ModelMap modelMap) throws Exception { modelMap.put("message", user); return new ModelAndView("/hello", modelMap); } //访问下面方法地址:http://localhost:8080/Spring-mvc/hello @RequestMapping(value="/hello1",method=RequestMethod.GET) public String test1(){ return "index"; } }
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> Hello,welcome learn Spring MVC,the message is ${message } <%=request.getAttribute("message") %> 测试user对象:${user.userName } </body> </html>
//访问下面方法地址:http://localhost:8080/Spring-mvc/hello @RequestMapping(value="/hello1",method=RequestMethod.GET) public String test1(){ return "index"; }
//访问下面方法地址:http://localhost:8080/Spring-mvc/login/wuxin @RequestMapping(value = "/login/{user}", method = RequestMethod.GET) public ModelAndView myMethod(HttpServletRequest request,HttpServletResponse response, @PathVariable("user") String user1, ModelMap modelMap) throws Exception { modelMap.put("message", user1); return new ModelAndView("/hello", modelMap); }上面方法中,我们返回了一个ModelAndView,它是Spring-mvc中的一个模型视图结合类,可以在我们要跳转的页面中加入ModelMap参数传递到页面中,上面的RequestMapping中value = "/login/{user}",user在方法中表示一个参数变量,而且是一个字符串类型的,@PathVariable("user") String user1,@PathVariable("user")中的user表示用户传入参数,要和上面value = "/login/{user}"保持一致,而user1表示方法接收的参数,用户传来String类型的参数user,将会传递给方法myMethod方法中的user1,然后程序接收处理
//以下是获取请求参数 /** * 通过@PathVariabl注解获取路径中传递参数 * 通过ModelMap向页面传值 * 访问下面方法地址:http://localhost:8080/Spring-mvc/hello.do/1/xin * @param id * @param userName * @return */ @RequestMapping(value="/hello2.do/{id}/{userName}") public String test2(@PathVariable int id,@PathVariable String userName){ System.out.println("id="+id+",userName="+userName); return "hello"; }
/** * 直接用HttpServletRequest获取 * 访问下面方法地址:http://localhost:8080/Spring-mvc/hello3.do?id=2&userName=xin * @param request * @param response * @return */ @RequestMapping(value="/hello3.do") public String test3(HttpServletRequest request,HttpServletResponse response){ int id=Integer.parseInt(request.getParameter("id")); String userName=request.getParameter("userName"); System.out.println("id="+id+",userName="+userName); return "hello"; }
/** * 用注解@RequestParam绑定请求参数id,userName到变量id,userName * 当请求参数userName不存在时会有异常发生,可以通过设置属性required=false解决, * 例如: @RequestParam(value="userName",required=false),请求参数属性为userName * String userName:接收参数属性为userName * 访问下面方法地址:http://localhost:8080/Spring-mvc/hello4.do?id=2&userName=xin * @param id * @param userName * @return */ @RequestMapping(value="/hello4.do") public String test4(@RequestParam("id")int id,@RequestParam(value="userName",required=false)String userName){ System.out.println("id="+id+",userName="+userName); return "hello"; }
/** * 封装为一个user类 * @author dell * */ public class User { private int id; private String userName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
/** * 以下方式可使用于表单提交 * 自动注入Bean属性(用@ModelAttribute注解获取POST请求的FORM表单数据) * 访问下面方法地址:http://localhost:8080/Spring-mvc/hello5.do?id=2&userName=xin * 经测试发现,参数中没有@ModelAttribute("user")也能够进行自动注入 * @param user * @return */ @RequestMapping(value="/hello5.do") public String test5(@ModelAttribute("user")User user){ System.out.println("id="+user.getId()+",userName="+user.getUserName()); return "hello"; }
<form action="hello5.do" method="post"> <input type="text" name="id"/> <input type="text" name="userName"/> <input type="submit" value="提交"/> </form>
/** * ModelAndView数据会利用HttpServletRequest的Attribute传值到hello.jsp中 * 访问下面方法地址:http://localhost:8080/Spring-mvc/hello6.do */ @RequestMapping(value="/hello6.do") public ModelAndView test6(){ Map<String,Object> data=new HashMap<String,Object>(); data.put("message", "test6-->xin"); return new ModelAndView("/hello", data); }
/** * 使用ModelMap作为参数对象示例: ModelMap数据会利用HttpServletRequest的Attribute传值到hello.jsp中 访问下面方法地址:http://localhost:8080/Spring-mvc/hello7.do * @return */ @RequestMapping(value="/hello7.do") public String test7(ModelMap map){ //以下两种方式都可以 //map.addAttribute("message", "test7-->xin"); map.put("message", "test7-->xin"); return "hello"; }
/** * 使用@ModelAttribute示例 在Controller方法的参数部分或Bean属性方法上使用 @ModelAttribute数据会利用HttpServletRequest的Attribute传值到success.jsp中 访问下面方法地址:http://localhost:8080/Spring-mvc/hello8.do * @param user * @return */ @RequestMapping(value="/hello8.do") public String test8(@ModelAttribute("user")User user){ user.setUserName("test8-->xin"); return "hello"; }
/** * Session存储:可以利用HttpServletReequest的getSession()方法 * 访问下面方法地址:http://localhost:8080/Spring-mvc/hello9.do * @return */ @RequestMapping(value="/hello9.do") public String test9(HttpServletRequest request){ request.setAttribute("message", "test9-->xin"); //request.getSession().setAttribute("message", "test9-->xin"); return "hello"; }
@RequestMapping(value="/hello9.do") public String test9(HttpServletRequest request){ String userName=request.getParameter("userName"); request.setAttribute("message", userName); //request.getSession().setAttribute("message", "test9-->xin"); return "hello"; }
@RequestMapping(value="/hello10.do") public ModelAndView test10(){ RedirectView view=new RedirectView("hello9.do"); return new ModelAndView(view); }2、使用redirect:前缀
//工作中常用的方法 @RequestMapping(value="/hello11.do") public String test11(){ return "redirect:hello9.do"; }输入测试地址:http://localhost:8080/spring-mvc/hello10.do,重定向到hello9.do方法中
标签:
原文地址:http://blog.csdn.net/harderxin/article/details/42235437