一、概述。
在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架也是界面把信息传输到Contorller层的一种架构,通过这个架构可以让我们把页面和Contorller层解耦,使得开发人员的分工更加明确。
二、代码演示。
1、首先配置SpringMVC环境。
1.1导入jar。
1.2、xml配置文件。
web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.springapp.mvc"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
2、前台界面代码。
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <form action="/method" method="get"> username:<input type="text" name="username" value=""/> <br/> password:<input type="text" name="password" value=""/> <br/> <input type="submit" value="登陆"/> </form> <hr/> <a href="/method?username=123&password=456">username</a> <br/> </body> </html>
登陆成功页面
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <hr/> <h1>Successful</h1> username:<%=request.getSession().getAttribute("name")%> <br/> password:<%=request.getSession().getAttribute("password")%> </body> </html>
3、Contorller层接收前台的两种方式。
方式一:
利用@RequestParam这个注解view plai co
package com.springapp.mvc; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Hello world!"); return "hello"; } @RequestMapping(value = "method",method = RequestMethod.GET) public String method(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest request) { System.out.println("come from the page " + username + " " + password); request.getSession().setAttribute("name", username); request.getSession().setAttribute("password",password); return "success"; } }方式二:
方式二中若不加前面的@RequestParam则String后面的变量名一定要与前台输入的变量名称相符合。
否者会报错。也就是说@RequestParam中的参数是从前台表单或者是链接中获取的,赋值给后面的类型对应的变量。
package com.springapp.mvc; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Hello world!"); return "hello"; } @RequestMapping(value = "method",method = RequestMethod.GET) public String method(/*@RequestParam("username")*/ String username, /*@RequestParam("password")*/ String password, HttpServletRequest request) { System.out.println("come from the page " + username + " " + password); request.getSession().setAttribute("name", username); request.getSession().setAttribute("password",password); return "success"; } }
4、界面结果。
第一种传值方式:
点击登陆后跳转的结果。
点击超链接后跳转的结果
第二种传值方式:
点击登陆后跳转的结果。
点击超链接后跳转的结果。
三、总结。
这里体现出了SpringMVC传值方式的多样性满足了开发人员的不同需求。第一种用来表单的提交。第二种用来界面间相互传值,也为了方便开发人员利用AJAX。