最新整合maven+SSM+Tomcat 实现注册登录

标签: spring mvcmybatistomcatspringmaven
6147人阅读 评论(11) 收藏 举报
分类:

mybatis学习 http://www.mybatis.org/mybatis-3/zh/index.html
Spring学习:http://blog.csdn.net/king1425/article/details/65631607
SpringMVC学习:http://blog.csdn.net/king1425/article/details/68922706

先看效果:
这里写图片描述

之后会有源代码,最好下载并对着代码观看

使用maven管理项目,需要注意修改编译输出目录。否则后期运行可能找不到类。
这里写图片描述

1.先建立必要的包。
如图所示:

这里写图片描述

2.根据数据库建立必要的表。
3.在pop.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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ws</groupId>
  <artifactId>FAQ</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>lavaFAQ</name>
  <url>http://maven.apache.org</url>

 <build>  
        <finalName>faq</finalName>  
        <plugins>  

        <plugin>         
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <version>2.3.1</version>  
            <configuration>  
            <source>1.6</source>  
            <target>1.6</target>  
            <encoding>utf8</encoding>    
            </configuration>  
        </plugin> 

        </plugins>  

     <resources>
           <resource>
              <directory>src/main/java</directory>
              <includes>
              <include>**/*.xml</include>
              </includes>
           </resource>
           <resource>
              <directory>src/main/resources</directory>
           </resource>
     </resources>

    </build>  


    <properties>  
            <!-- spring版本号 -->  
            <spring.version>4.0.2.RELEASE</spring.version>  
            <!-- mybatis版本号 -->  
            <mybatis.version>3.2.6</mybatis.version>  
            <!-- log4j日志文件管理包版本 -->  
            <slf4j.version>1.7.7</slf4j.version>  
            <log4j.version>1.2.17</log4j.version>  
        </properties>  

        <dependencies>  
            <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.11</version>             

            </dependency>  
            <!-- spring核心包 -->  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  

            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-web</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-oxm</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-tx</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  

            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-jdbc</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  

            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-webmvc</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-aop</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  

            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context-support</artifactId>  
                <version>${spring.version}</version>  
            </dependency>


            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-test</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
            <!-- mybatis核心包 -->  
            <dependency>  
                <groupId>org.mybatis</groupId>  
                <artifactId>mybatis</artifactId>  
                <version>${mybatis.version}</version>  
            </dependency>  
            <!-- mybatis/spring包 -->  
            <dependency>  
                <groupId>org.mybatis</groupId>  
                <artifactId>mybatis-spring</artifactId>  
                <version>1.2.2</version>  
            </dependency>  
            <!-- 导入java ee jar 包 -->  
            <dependency>  
                <groupId>javax</groupId>  
                <artifactId>javaee-api</artifactId>  
                <version>7.0</version>  
            </dependency>  
            <!-- 导入Mysql数据库链接jar包 -->  
            <dependency>  
                <groupId>mysql</groupId>  
                <artifactId>mysql-connector-java</artifactId>  
                <version>5.1.30</version>  
            </dependency>  
            <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->  
            <dependency>  
                <groupId>commons-dbcp</groupId>  
                <artifactId>commons-dbcp</artifactId>  
                <version>1.2.2</version>  
            </dependency>  
            <!-- JSTL标签类 -->  
            <dependency>  
                <groupId>jstl</groupId>  
                <artifactId>jstl</artifactId>  
                <version>1.2</version>  
            </dependency>  
            <!-- 日志文件管理包 -->  
            <!-- log start -->  
            <dependency>  
                <groupId>log4j</groupId>  
                <artifactId>log4j</artifactId>  
                <version>${log4j.version}</version>  
            </dependency>  


            <!-- 格式化对象,方便输出日志 -->  
            <dependency>  
                <groupId>com.alibaba</groupId>  
                <artifactId>fastjson</artifactId>  
                <version>1.1.41</version>  
            </dependency>  

            <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
            <dependency>
               <groupId>com.google.code.gson</groupId>
               <artifactId>gson</artifactId>
               <version>2.7</version>
            </dependency>

            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-api</artifactId>  
                <version>${slf4j.version}</version>  
            </dependency>  

            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-log4j12</artifactId>  
                <version>${slf4j.version}</version>  
            </dependency>  
            <!-- log end -->  
            <!-- 映入JSON -->  
            <dependency>  
                <groupId>org.codehaus.jackson</groupId>  
                <artifactId>jackson-mapper-asl</artifactId>  
                <version>1.9.13</version>  
            </dependency>  
            <!-- 上传组件包 -->  
            <dependency>  
                <groupId>commons-fileupload</groupId>  
                <artifactId>commons-fileupload</artifactId>  
                <version>1.3.1</version>  
            </dependency>  
            <dependency>  
                <groupId>commons-io</groupId>  
                <artifactId>commons-io</artifactId>  
                <version>2.4</version>  
            </dependency>  
            <dependency>  
                <groupId>commons-codec</groupId>  
                <artifactId>commons-codec</artifactId>  
                <version>1.9</version>  
            </dependency>    

               <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
            <dependency>
               <groupId>javax.mail</groupId>
               <artifactId>mail</artifactId>
               <version>1.4</version>
            </dependency>                   
           </dependencies>   

        <!-- added by wangshuo end -->
</project>

然后执行 maven install。成功后配置SSM。
建立 spring-mybatis.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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop.xsd">

     <!-- 自动扫描 -->  
    <context:component-scan base-package="com.lava" />

    <!-- 引入配置文件 -->  
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:jdbc.properties" />  
    </bean>  

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driverClassName}" />  
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>  
    </bean>  

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:com/lava/lavafaq/mapping/*.xml"></property>  
    </bean>  

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.lava.lavafaq.dao" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>  

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>    

</beans>

建立 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:p="http://www.springframework.org/schema/p"
    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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">



    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="com.lava.lavafaq.controller" />


    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />   <!-- JSON转换器 -->
            </list>
        </property>
    </bean>
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <!-- <property name="prefix" value="/WEB-INF/jsp/" /> -->
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="1" />
    </bean>

    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean> 

  <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 --> 
    <mvc:annotation-driven/>
    <!-- 启用基于Spring的注解 -->
    <context:annotation-config/>
    <!-- 静态资源处理 -->
    <mvc:resources mapping="/script/**" location="/script/" />  
    <mvc:resources mapping="/app/**" location="/app/" />  
    <mvc:resources mapping="/css/**" location="/css/" />  
    <mvc:resources mapping="/images/**" location="/images/" /> 


    <!-- 国际化配置 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
        <property name="basename"> 
            <value>message</value> 
        </property> 
        <property name="defaultEncoding"> 
            <value>UTF-8</value> 
        </property> 
    </bean> 

    <!-- 日期全局转换配置 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">    
         <property name="converters">    
             <list>    
                 <bean class="com.lava.lavafaq.springmvc.DateConverter" />    
             </list>    
         </property>    
     </bean>  

</beans> 

然后在 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_3_0.xsd"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <!-- Spring和mybatis的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>
    <!-- 编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- Spring MVC servlet -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置SESSION超时,单位是分钟 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>

然后建立 jdbc.properties和log4J.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/faq
username=root
password=123456
initialSize=10
maxActive=300
maxIdle=20
minIdle=1
maxWait=60000


 log4j.rootLogger=DEBUG,Console,File  
#定义日志输出目的地为控制台  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
#可以灵活地指定日志输出格式,下面一行是指定具体的格式  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  

#文件大小到达指定尺寸的时候产生一个新的文件  
log4j.appender.File = org.apache.log4j.RollingFileAppender  
#指定输出目录  
log4j.appender.File.File = logs/ssm.log  
#定义文件最大大小  
log4j.appender.File.MaxFileSize = 10MB  
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志  
log4j.appender.File.Threshold = ALL  
log4j.appender.File.layout = org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

至此配置完成。配置是SSM中至关重要的部分,只要配置成功,接下来很容易
然后写入SSM逻辑代码(看源码)。
然后建立测试类,测试Spring与mybatis

@RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类  
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) 

public class TestMyBatis {  
   private static Logger logger = Logger.getLogger(TestMyBatis.class);  
 //private ApplicationContext ac = null;  
   @Resource  
   private IPersonService personService = null;  

/* @Before  
 public void before() {  
     ac = new ClassPathXmlApplicationContext("applicationContext.xml");  
     userService = (IUserService) ac.getBean("userService");  
 } */ 

   @Test  
   public void test1() { 
        Person person = new Person();   
        person.setMail("114336");
        person.setPassword("114336");

       logger.info(personService.loginPerson(person));  
       logger.info("ws");
   }  

如果控制台有打印personService.loginPerson(person)返回值,即成功(之后会有源代码,对着代码观看)

然后测试spring-mvc
浏览器地址栏输入:
输入地址:localhost:8080/项目名称/user/login 跳转至登录界面即成功。

下面走一遍代码实现逻辑流程
输入完毕,点击注册按钮。执行Ajax

login.js

$.ajax({
            type: reMethod,
            url: "/ssm/login/person",
            data: sendData,
            dataType: "json",
            success: function(result) {             
                if (result.success) {
                    $('#user').focus().css({
                        border: "1px solid green",
                        boxShadow: "0 0 2px green"
                    });
                    $("#userCue").html("<font color='green'><b>"+result.text+"</b></font>");
                    alert(result.text);                 
                    return false;
                } else {
                    $('#user').css({
                        border: "1px solid #D7D7D7",
                        boxShadow: "none"
                    });
                    $("#userCue").html("<font color='red'><b>"+result.text+"</b></font>");
                    alert(result.text); 
                }

            }
        });

spring mvc 拦截 url: “/ssm/login/person”,

@Controller  
@RequestMapping("/login")  
public class LoginController {

.........

@RequestMapping("/person")
    @ResponseBody
    private Object register(Person person){

       logger.debug("ws--register-------"); 
       person.setState(0);
       return ResponseUtils.sendSuccess(this.personService.registerPerson(person));      
    }

数据合法则发邮件给用户,等待激活。

package com.ws.core.utils;

import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

import com.cn.hnust.been.Person;
import com.cn.hnust.controller.LoginController;
import com.sun.mail.util.MailSSLSocketFactory;



public class MailUtils {
    private static Logger logger = Logger.getLogger(MailUtils.class);
    //--------------参数---------------------
    public static final String FROM = "1143368701@qq.com";//发件人的email
    public static final String PWD = "*********";//发件人密码--邮箱密码,填写自己的
    public static final String URL = "http://localhost:8080/ssm/login";//项目主页
    public static final int TIMELIMIT = 1000*60*60*24; //激活邮件过期时间24小时
    public static final String TITLE = "FAQ账户激活邮件";

//---------------自定义函数-----------------
    public static Person sendMail(Person u) throws AddressException, MessagingException, NoSuchAlgorithmException {
        //注册邮箱
        String to  = u.getMail();
        //当前时间戳
        Long curTime = System.currentTimeMillis();
        //激活的有效时间
        Long activateTime = curTime+TIMELIMIT;
        //激活码--用于激活邮箱账号       
        u.setActiCode(UUIDUtils.getUUID()+curTime);     
        String actiCode = u.getActiCode();
        //过期时间       
        u.setToken_exptime(String.valueOf(activateTime));
        //发送的邮箱内容
        String content = "<p>您好 O(∩_∩)O~~<br><br>欢迎加入FAQ!<br><br>帐户需要激活才能使用,赶紧激活成为FAQ正式的一员吧:)<br><br>请在24小时内点击下面的链接立即激活帐户:"
                +"<br><a href='"+URL+"/activatemail/?actiCode="+actiCode+"&email="+to+"'>"
                +URL+"/activatemail/?actiCode="+actiCode+"&email="+to+"</a></p>";
        //调用发送邮箱服务
        sendMailEntity(to, TITLE, content);
        return u;
    }


     public static void sendMailEntity(String to,String title,String content) throws AddressException, MessagingException {

            Properties props = new Properties(); //可以加载一个配置文件      
            // 设置邮件服务器主机名
            props.setProperty("mail.host", "smtp.qq.com");//指定邮件服务器,默认端口 25
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");//要采用指定用户名密码的方式去认证
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");

         // 开启SSL加密,否则会失败
            MailSSLSocketFactory sf = null;
            try {
                sf = new MailSSLSocketFactory();
            } catch (GeneralSecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);

            //根据属性新建一个邮件会话  
            Session session = Session.getInstance(props,new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // TODO Auto-generated method stub
                    return new PasswordAuthentication(FROM, PWD);
                }
            });
            session.setDebug(true); //有他会打印一些调试信息。  
            MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象  
            message.setFrom(new InternetAddress(FROM));//设置发件人的地址  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//设置收件人,并设置其接收类型为TO            
            message.setSubject(title,"UTF-8");//设置标题  
            //设置信件内容  
            //message.setText(content,"UTF-8"); //发送 纯文本 邮件 todo  
            message.setContent(content, "text/html;charset=utf-8"); //发送HTML邮件,内容样式比较丰富  
            message.setSentDate(new Date());//设置发信时间  
            //message.saveChanges();//存储邮件信息  
            //发送邮件  
            Transport.send(message);     
            logger.debug("Transport.send"); 
            /*//发送邮件 (二)
            Transport transport = session.getTransport(SMTP);  
            //Transport transport = session.getTransport();  
            transport.connect(FROM, PWD);
            transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址  
            transport.close(); */ 
        }

}

点击登录按钮,通过表单提交

<div class="login_form">
                <form action="<%=basePath%>login/index" name="loginform" accept-charset="utf-8" id="login_form" class="loginForm" method="post"><input type="hidden" name="did" value="0"/>
               <input type="hidden" name="to" value="log"/>

               <div class="loginHint" id="loginHint">${param.result}</div>

                <div class="uinArea" id="uinArea">
                <label class="input-tips" for="u">帐号:</label>
                <div class="inputOuter" id="uArea">

                    <input type="text" id="u" name="mail" class="inputstyle"/>
                </div>
                </div>
                <div class="pwdArea" id="pwdArea">
               <label class="input-tips" for="p">密码:</label> 
               <div class="inputOuter" id="pArea">

                    <input type="password" id="p" name="password" class="inputstyle"/>
                </div>
                </div>

                <div style="padding-left:50px;margin-top:20px;"><input type="submit" value="登 录" style="width:150px;" class="button_blue"/></div>
              </form>
           </div>

String mvc拦截登录请求

    @RequestMapping("/index")   
    private String login(Person person,ModelMap modelMap){  
         logger.debug("ws-----activatemail----person.getPassword();="+person.getPassword()+" email="+person.getMail());   
        String result = this.personService.loginPerson(person);
        if(result.equals("登陆成功")){
             return "index";    
        }
         modelMap.addAttribute("result",result);
         logger.debug("ws-----login----result="+result); 
       return "redirect:/user/login";        
    }

精简SSM demo下载 http://download.csdn.net/detail/king1425/9800469
全面SSM demo https://github.com/WangShuo1143368701/JavaWebFAQ

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:106912次
    • 积分:2408
    • 等级:
    • 排名:第16017名
    • 原创:129篇
    • 转载:2篇
    • 译文:0篇
    • 评论:87条
    联系方式
    博客专栏
    最新评论