Maven项目的简单应用(登录)

Maven项目建好后配置DispatcherServlet后,实现简单登录操作

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>Archetype Created Web Application</display-name>
<servlet>
	<description>
	</description>
	<display-name>DispatcherServlet</display-name>
	<servlet-name>DispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<description>
		</description>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:conf/spring-*.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
	    <servlet-name>DispatcherServlet</servlet-name>
	    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

在src/main/resources下新建文件夹conf,在conf文件夹下新建spring-service.xml文件
在这里插入图片描述
在src/main/java下新建包,名称为com.test.controller,在新建包下新建UserController类在这里插入图片描述
在spring-service配置扫描,代码如图

<?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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	 
	  <!-- 包自动扫描 -->
	  <context:component-scan base-package="com.test" ></context:component-scan>
	  <!-- 启动注解版的Spring MVC -->
	  <mvc:annotation-driven></mvc:annotation-driven>   

</beans>

实现UserController类扫描到Spring容器里,需要配置注解 @Controller或**@Component**

在UserController类中实现登录方法前,要给方法和类配置请求映射**@RequestMapping()**

将返回内容输出到网页端需要配置注解**@ResponseBody**
我们通过ajax访问controller,实现页面和 web 服务器之间数据的传输

UserController代码如下

@Controller//将该类扫描到spring容器中
@RequestMapping("/user")//UserController类的映射路径
public class UserController {

	/*
	 * 登录方法
	 * */
	@RequestMapping("/login")//将login方法映射
	@ResponseBody//将返回内容输出到网页端
	public String login(String name,String password) {
		System.out.println("账号:"+name);
		System.out.println("密码:"+password);
		return "hello world";
	}
	
	
}

login.html代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
    <script type="text/javascript"  src="js/jquery-2.1.1.min.js"></script>
    <script>
     $(function(){
    	 $('#login').click(function(){
    		var sendDate={"name":$('#name').val(),"password":$('#ps').val()};
        	$.ajax({
        		type:"post",
        		url:"user/login.do",
        		data:sendDate,
        		datatype:"json",
        		success:function(msg){
        			console.log(msg);
        		},
        		error:function(msg){
        			alert('通信失败');
        		}
        	});
        })
     });
    </script>
</head>
<body>
    
    
        <div id="form">
            <div><input type="text" placeholder="名称" id="name"></div>
            <div><input type="password" placeholder="密码" id="ps"></div>
                
            <button id="login">登录</button>
            
        </div>
    
    
</body>
</html>

注意:UserController中的login方法中的参数名称要跟sendDate的键名称一致

运行在这里插入图片描述
运行结果在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下﹁秒待續

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值