1. 基于注解的SpringMVC应用
(1)编程步骤
step1.导包。
spring-webmvc
step2.添加spring配置文件。
step3.配置DispatcherServlet。
step4.写Controller
step5.写jsp
step6.在配置文件当中,添加ViewResolver配置,添加组件扫描,添加MVC注解扫描。
(2).读取请求参数值
1)方式一 通过request提供的方法
2) 方式二 通过@RequestParam注解
3) 方式三 通过javabean
step1. 先写一个java类
step2. 将该类作为方法参数
(3)向页面传值
1)方式一 将数据绑订到request
2)方式二 返回ModelAndView
3)方式三 将数据添加到ModelMap
4)方式四 将数据绑订到session
(4)重定向
1)方法的返回值是String
比如 return "redirect:toIndex.do";
2)方法的返回值是ModelAndView
比如RedirectView rv = new RedirectView("toIndex.do");ModelAndView mav = new ModelAndView(rv);
代码演示:
/springmvc-day02/pom.xml
<project xmlns="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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>welkin</groupId><artifactId>springmvc-day02</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.2.8.RELEASE</version></dependency></dependencies>
</project>
/springmvc-day02/src/main/java/controller
adminParam.java
package controller;
public class adminParam {private String adminCode;private String pwd;public String getAdminCode() {return adminCode;}public void setAdminCode(String adminCode) {this.adminCode = adminCode;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}}
HelloController.java
package controller;import java.util.HashMap;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;/*** 处理器类 * 1.不用实现Controller * 2.方法名不做要求,返回值可以是ModelAndView,也可以是String * 3.可以添加多个方法* 4.使用@Controller * 5.可以在方法前或者类前添加@RequestMapping* http://ip:port/springmvc-day02/hello.do*/
@Controller
public class HelloController {
@RequestMapping("/hello.do")public String hello() {System.out.println("hello()");return "hello";}@RequestMapping("/toLogin.do")public String toLogin() {System.out.println("toLogin()");return "login";}@RequestMapping("/login.do")public String login(HttpServletRequest request) {System.out.println("login()");String adminCode = request.getParameter("adminCode");System.out.println("adminCode:" + adminCode);return "index";}@RequestMapping("/login2.do")public String login2(String adminCode,@RequestParam("pwd") String pwd){System.out.println("login2()");System.out.println("adminCode:"+adminCode+"\n"+"pwd:"+pwd);return "index"; }@RequestMapping("/login3.do")public String login3(adminParam ap){System.out.println("login3()");String adminCode = ap.getAdminCode();String pwd = ap.getPwd();System.out.println("adminCode:"+adminCode+"\n"+"pwd:"+pwd);return "index";}@RequestMapping("/login4.do")public String login4(adminParam ap,HttpServletRequest request){System.out.println("login4()");String adminCode = ap.getAdminCode();request.setAttribute("adminCode", adminCode);return "index"; }@RequestMapping("/login5.do")public ModelAndView login5(adminParam ap){System.out.println("login5()");String adminCode = ap.getAdminCode();Map<String,Object> data = new HashMap<String, Object>();data.put("adminCode", adminCode);ModelAndView mav = new ModelAndView("index",data);return mav;}@RequestMapping("/login6.do")public String login6(adminParam ap,ModelMap mm){System.out.println("login6()");String adminCode = ap.getAdminCode();mm.addAttribute("adminCode",adminCode);return "index";}@RequestMapping("/login7.do")public String login7(adminParam ap,HttpSession session){System.out.println("login7()");String adminCode = ap.getAdminCode();session.setAttribute("adminCode", adminCode);return "index";}@RequestMapping("/login8")public String login8(){System.out.println("login8()");return "redirect:toIndex.do";}@RequestMapping("/toIndex")public String toIndex(){System.out.println("toIndex()");return "index";}@RequestMapping("/login9.do")public ModelAndView login9(){System.out.println("login9()");RedirectView rv = new RedirectView("toIndex.do");ModelAndView mav = new ModelAndView(rv);return mav;}@RequestMapping("/toBmi.do")public String toBmi() {System.out.println("toBmi()");return "bmi";}@RequestMapping("/bmi.do")public String bmi(HttpServletRequest request) {System.out.println("bmi()");String height = request.getParameter("height");String weight = request.getParameter("weight");double bmi = Double.parseDouble(weight) / Double.parseDouble(height) / Double.parseDouble(height);String status = "正常";if (bmi < 19) {status = "过轻";}if (bmi > 25) {status = "过重";}request.setAttribute("status", status);request.setAttribute("bmi", bmi);return "view";}
}
/springmvc-day02/src/main/resources/spring-mvc.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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><context:component-scan base-package="controller"></context:component-scan><mvc:annotation-driven /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/" /><property name="suffix" value=".jsp" /></bean>
</beans>
/springmvc-day02/src/main/webapp/WEB-INF
bmi.jsp
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body style="font-size: 30px;"><form action="bmi.do" method="post">身高(米):<input name="height" /><br />体重(公斤):<input name="weight" /><br /><br /> <input type="submit" value="确定" /></form>
</body>
</html>
hello.jsp
<h1>hello,SpringMVC!</h1>
index.jsp
<h1>index</h1>
adminCode:${adminCode }
login.jsp
<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<html><head></head><body style="font-size:30px;"><form action="login9.do" method="post">账号:<input name="adminCode"/><br/>密码:<input type="password" name="pwd"/><br/><input type="submit" value="登录"/></form></body>
</html>
view.jsp
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body style="font-size: 30px;">BMI指数:${bmi } 状态:${status}
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern>
</servlet-mapping></web-app>