sping+springmvc+mybatis登录

来源:互联网 发布:知乎 坠井 编辑:程序博客网 时间:2024/06/05 04:50

1.spring+mybatis代码

spring+mybatis代码

2.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>springMVCDemo</display-name>      <!-- 监听spring上下文容器 -->      <listener>          <listener-class>              org.springframework.web.context.ContextLoaderListener          </listener-class>      </listener>             <!-- 加载spring的xml配置文件到 spring的上下文容器中 -->      <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:spring-mybatis.xml</param-value>      </context-param>           <!-- 配置DispatchcerServlet -->    <servlet>        <servlet-name>springDispatcherServlet</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>          <load-on-startup>1</load-on-startup>     </servlet>         <servlet-mapping>        <servlet-name>springDispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>       <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></web-app>
3.controller

/** *  */package com.jit.test.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.jit.test.model.User;import com.jit.test.servicedao.ServiceDao;import com.jit.test.servicedaoimpl.ServiceDaoImpl;/** * @author sue wong * 2017年8月6日下午1:49:26 * since v1.0 */@Controllerpublic class LoginController {@Autowiredprivate ServiceDao servicedao;//private User user;private String s;/** * @return the servicedao */public ServiceDao getServicedao() {return servicedao;}/** * @param servicedao the servicedao to set */public void setServicedao(ServiceDao servicedao) {this.servicedao = servicedao;}@RequestMapping("/login")public String Login(String username,String password){System.out.println("sssssssss");try {User u=new User();u.setUsername(username);u.setPassword(password);System.out.println(u.getUsername());if(servicedao.validLogin(u)){s="success";}else{s= "error";}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return s;}}


4.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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/aop     http://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx.xsd    http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">          <!-- 配置自动扫描的包 -->         <context:component-scan base-package="com.jit.test.controller"></context:component-scan>                  <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">             <property name = "prefix" value="/"></property>             <property name = "suffix" value = ".jsp"></property>         </bean>        </beans>

5.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><form action="login" method="post">    username:<input type="text" name = "username" ><p>     password:<input type="password" name = "password" ><p>    <input type="submit" value="登录"> </form></body></html>

6.效果图



原创粉丝点击