SpringMvc接受请求方式的几种方法(所有代码集合)

学习目标:掌握接受请求的几种方法

前言:
本项目实现效果为进入index界面显示已登录,请登录和未注册,请注册两个链接
点击后分别跳转到login.jsp和register.jsp中在register.jsp中实现判断账号是否为学号
学号不正确跳转到index.jsp界面并提示,学号正确跳转到login.jsp界面并显示账号;
项目主要学习多种接受请求方法。


代码:

项目结构图:
在这里插入图片描述

导入16个基本jar包:
在这里插入图片描述
配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>mvc-4-12</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  		<!-- 初始化参数,配置Spring MVC配置文件的位置以及名称 -->
  		<init-param>
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath:springmvc.xml</param-value>
  		</init-param>
  		<!-- 表示容器启动时,立即加载dispatcherServlet -->
  		<load-on-startup>1</load-on-startup>  		
  </servlet>
    <!-- Spring。  MVC的前端控制器拦截所有的请求 -->
  <servlet-mapping>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

配置springmvc.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 配置处理器Handle,映射为“/hello”的请求 -->
	<!--  <bean name="/login" class="controller.HelloController" /> 
	 <bean name="/verify" class="controller.VerifyLoginController" />
	 <bean name="/register" class="controller.RegisterController" />   -->
	 <context:component-scan base-package="controller"></context:component-scan><!--配置扫描包*/-->
	<!-- 配置视图解析器,将控制器方法返回的逻辑视图解析为物理视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
	 <property name="prefix" value="/jsp/"></property> <!--前缀-->
	<property name="suffix" value=".jsp"></property><!--后缀-->
	</bean>
</beans>

UserForm实体类:

package model;

public class UserForm {
	String username;
	String pwd;
	String tel;
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}
	/**
	 * @param pwd the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	/**
	 * @return the tel
	 */
	public String getTel() {
		return tel;
	}
	/**
	 * @param tel the tel to set
	 */
	public void setTel(String tel) {
		this.tel = tel;
	}
	
}

index.jsp:

<%@ 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>
  ${msg} <br>
     已注册,请登录<a href="jsp/login.jsp">登录</a><br>
    未注册,请注册<a href="jsp/register.jsp">注册 </a><br>
  </body>
</html>

在这里插入图片描述
register.jsp:

<%@ 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 'register.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>
       <form  action="hello/register" method="post">
 <!--        <form  action="hello/register" method="get"> 
       <form  action="hello/register/1931030119">  -->     
	用户名:<input type="text" name="username"><br>                   
	密码:<input type="password" name="pwd"><br>
	年龄:<input type="text" name="tel"><br>                                              
       <input type="submit"  value="注册"/>                    
       <input type="reset" value="重置"> 
</form>
  </body>
</html>

在这里插入图片描述
login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="fm" uri="http://www.springframework.org/tags/form" %>
<%
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 'login.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>
<%--   ${sessionScope.msg} <br>
  ${requestSession.msg} <br> --%>
  ${msg} <br>
           <form >                    
用户名:<input type="text" name="username" value="${msg1}"><br>                   
密码:<input type="password" name="pwd"><br>                                            
       <input type="submit"  value="登录"/>                    
       <input type="reset" value="重置"> 
</form>
  </body>
</html>

在这里插入图片描述
RegisterController:

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserForm;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
/*@SessionAttributes(value={"msg"})*/
@RequestMapping(value="/hello")
public class RegisterController {
	//判断注册如果账号是学号跳转到login @RequestParam方法
/*	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(@RequestParam("username") String username,@RequestParam("pwd") String pwd
			,@RequestParam("age") String age,Model m){
		if(username.equals("1931030119")){
			m.addAttribute("msg","这是***的登录界面");
			m.addAttribute("msg1",username);
			return "login";
		}else{
			m.addAttribute("msg","提示:账号命名不规范,请重新注册");
			return "index";
		}
	}*/
//实体类方法
/*	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(UserForm user,Model m){
		if(user.getUsername().equals("1931030119")){
			m.addAttribute("msg","欢迎***");
			m.addAttribute("msg1",user.getUsername());
			return "login";
		}else{
			m.addAttribute("msg","提示:账号命名不规范,请重新注册");
			return "index";
		}
	}*/
//HttpServletRequest方法
/*	@RequestMapping(value="/register",method=RequestMethod.POST)
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(arg0.getParameter("username"));
		ModelAndView mView = new ModelAndView();
		if(arg0.getParameter("username").equals("1931030119")){
			mView.addObject("msg", "欢迎");
			mView.addObject("msg1",arg0.getParameter("username"));
			mView.setViewName("login");
			return mView;
		}else{
			mView.addObject("msg","提示:账号命名不规范,请重新注册");
			mView.setViewName("index");
			return mView;
		}
	}*/
//ModelAndView方法
/*	@RequestMapping(value="/register",method=RequestMethod.POST)
	public ModelAndView handleRequest(String username,String pwd) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView m = new ModelAndView();
		if(username.equals("1931030119")){
			m.addObject("msg","这是***的登录界面");
			m.setViewName("login");
			return m;
		}else{
			m.addObject("msg","提示:账号命名不规范,请重新注册");
			m.setViewName("index");
			return m;
		}
	}*/
	//形参方式
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register( String username, String pwd, String age,Model m){
		if(username.equals("1931030119")){
			m.addAttribute("msg","这是***的登录界面");
			m.addAttribute("msg1",username);
			return "login";
		}else{
			m.addAttribute("msg","提示:账号命名不规范,请重新注册");
			/*return "index";*/
			return "index";
		}
	}
	//httpservletrequest方法
/*	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(HttpServletRequest req, Model m){
		if(req.getParameter("username").equals("1931030119")){
			m.addAttribute("msg","这是***的登录界面");
			return "login";
		}else{
			m.addAttribute("msg","提示:账号命名不规范,请重新注册");
			return "index";
		}
	}*/
	//get方法
/*	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String register(@RequestParam("username") String username,@RequestParam("pwd") String pwd
	,@RequestParam("age") String age,Model m){
		if(username.equals("1931030119")){
			m.addAttribute("msg","这是***的登录界面");
			return "login";
		}else{
			m.addAttribute("msg","提示:账号命名不规范,请重新注册");
			return "index";
		}
	}*/
//rest方法	
/*		@RequestMapping("/register/{username}")
		public String testRest(@PathVariable("username") String username,Model m){
			if(username.equals("1931030119")){
				m.addAttribute("msg","这是***的登录界面");
				return "login";
			}else{
				m.addAttribute("msg","提示:账号命名不规范,请重新注册");
				return "index";
			}
		}*/

}

问题解决:


重新导入jar包可能会解决该问题


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值