当前位置: 代码迷 >> 综合 >> SPRINGMYBATIS01 Unit04:实战技巧 、 登录
  详细解决方案

SPRINGMYBATIS01 Unit04:实战技巧 、 登录

热度:91   发布时间:2023-12-11 15:01:44.0

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;/** 用于封装请求参数值* 属性名与请求参数名一致* 注:* 名称要一样,类型要匹配(会做类型转换,比如将字符串转换成int)* 提供相应的get/set方法*/
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")/** DispatcherServlet在调用处理器的方法之前,会利用Java反射机制分析方法的结构,比如这儿,通过分析会将请求对象传过来(了解)。*/public String login(HttpServletRequest request) {System.out.println("login()");String adminCode = request.getParameter("adminCode");System.out.println("adminCode:" + adminCode);return "index";}@RequestMapping("/login2.do")/** DistpatcherServlet调用处理器的方法之前,会分析方法的结构。这儿,会调用request对象的方法,先获得参数值,然后赋给对应的形参。(了解)* 如果不一致,要@RequestParam注解*/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")/** DistpatcherServlet会将AdminParam对象创建好(会将请求参数值赋给相应的属性)。*/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);//默认情况下,DistpatcherServlet会使用转发。return "index";     }@RequestMapping("/login5.do")public ModelAndView login5(adminParam ap){System.out.println("login5()");String adminCode = ap.getAdminCode();//step1.将数据添加到Map对象里面Map<String,Object> data = new HashMap<String, Object>();//相当于执行了request.setAttributedata.put("adminCode", adminCode);//step2.将Map对象添加到ModelAndViewModelAndView 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();//相当于执行了request.setAttributemm.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注解扫描 --><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><!-- DispatcherServlet的初始化方法会启动spring容器,contextConfigLocation用来指定spring配置文件的位置。--><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>