• springmvc----demo---login---bai


    web.xml配置:
    
    <?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">
    	<servlet>
    		<servlet-name>springmvc</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>springmvc</servlet-name>
    		<url-pattern>*.html</url-pattern>
    	</servlet-mapping>
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    </web-app>
    ========================================================================
    
    springmvc-servlet.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:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    	
    	<context:component-scan base-package="com.etc.controller">
    	</context:component-scan>
    	
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
    		<property name="prefix" value="/WEB-INF/jsp/"/>	
    		<property name="suffix" value=".jsp"/>
    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>	
    	</bean>
    </beans>	
    
    ==========================================================================
    
    login.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 }
            <form action="login.html" method="post">
        用户名: <input name="username" /> <br/>
      密码:   <input name="password" type="password"/><br/>
         <input type="submit" value="登录"/>
        </form>
      </body>
    </html>
    ==========================================================================
    
    LoginAction:
    
    package com.etc.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    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.servlet.ModelAndView;
    
    @Controller
    public class LoginAction 
    {
    	@RequestMapping(value="/login",method=RequestMethod.POST)
    	public ModelAndView login(@RequestParam(value="username",required=true)String username
    			,@RequestParam(value="password",required=true)String password)
    	{
    		Map<String,String> model = new HashMap<String,String>();
    		ModelAndView ma = null;
    		//判断是否正确
    		if("admin".equals(username)&&"123".equals(password))
    		{
    			model.put("username", username);
    			ma = new ModelAndView("welcome", model);
    		}
    		else
    		{
    			model.put("msg", "用户名或密码错误!");
    			ma = new ModelAndView("../../login", model);
    		}
    		return ma;
    	}
    }
    ==========================================================================
    welcome.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 'welcome.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>
        登录成功,欢迎${username }。
      </body>
    </html>
    ==========================================================================
    

      

  • 相关阅读:
    go语言之goroute协程
    Vue中Computed和Watch的用法及区别
    php判断复选框是否被选中的方法
    基于workerman的实时推送
    织梦引入公共模板
    织梦快速建站首页模板
    golang解决中文乱码的方法
    Vue项目中使用可视化图表echarts
    解决for循环中异步请求顺序不一致的问题
    layui多图上传实现删除功能的方法
  • 原文地址:https://www.cnblogs.com/ipetergo/p/6270044.html
Copyright © 2020-2023  润新知