当前位置:编程学习 > JAVA >>

Spring学习笔记(1)----将Spring与Servlet整合

 之前一直都是将Spring与Struts2整合在一起,近日需要将Spring与Servlet整合在一起。如果把Spring与Struts2整合在一起的方法用到Servlet显然是不行的。因为Struts2已经与Servlet分离了,可以把Struts2的action类看成是一个普通类,可以用IOC的方式注入。但是Servlet是一个服务器类,servlet容器实例化一个对象是需要经过init()-àService()-àdoPost()||doGet()àdestroy()的过程,所以我们需要在init()时期就创建一个ApplicationContext对象并且得到注册在Spring配置文件中的bean对象。

     做了个登录小例子,贴出完整的代码。

     新建一个web工程,在web.xml添加如下所示配置:

[html]  www.zzzyk.com
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
           classpath*:applicationContext.xml 
       </param-value> 
  </context-param> 
  <listener> 
    <description>启动spring上下工厂的监听器</description> 
    <listener-class> 
          org.springframework.web.context.ContextLoaderListener 
       </listener-class> 
  </listener> 


      新建一个接口UserAccount.java,代码如下所示:

[java
package com.ldfsoft.service; 
  
public interface UserAccount { 
    public boolean checkAccount(String account, String password); 


 
      接着新建一个该接口的实现类checkAccount.java  ,代码如下所示:
 
[java]
package com.ldfsoft.serviceimp; 
  
import com.ldfsoft.service.UserAccount; 
  
public class UserAccountImp implements UserAccount { 
  
    @Override 
    public boolean checkAccount(Stringaccount, String password) { 
       // TODO Auto-generatedmethod stub 
       if("admin".equals(account)&&"123".equals(password)){ 
           return true; 
       } 
       return false; 
    } 
  


        将此类在Spring的配置文件applicationContext.xml配置一下,如下所示:

[html] 
<?xml version="1.0"encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
       
       <bean id="userAccount"class="com.ldfsoft.serviceimp.UserAccountImp"></bean> 
       
          
</beans> 


 
       接着新建一个登陆页面login.jsp,代码如下所示:
 
[html]
<%@ page language="java"contentType="text/html; charset=utf-8" 
    pageEncoding="utf-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<script type="text/javascript"> 
    function check(){ 
       var account=document.getElementById("account").value; 
       var password=document.getElementById("password").value; 
       if(account=="" || password==""){ 
           alert("用户名或者密码不能为空!"); 
           return false; 
       } 
       return true; 
    } 
</script> 
<meta http-equiv="Content-Type"content="text/html; charset=utf-8"> 
<title>登录</title> 
</head> 
<body> 
    <form action="LoginServlet" method="post"> 
       账户:<input type="text"id="account" name="account"><br> 
       密码:<input type="password"id="password" name="password"><br> 
       <input type="submit"value="登录" onclick="returncheck()"> 
    </form> 
</body> 
</html> 


         新建一个登陆成功的页面success.jsp,代码如下所示:

[html]
<%@ page language="java"contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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>登陆成功</title> 
</head> 
<body> 
    <%=request.getParameter("account") %>,欢迎您回来... 
</body> 
</html> 


      登陆失败的页面fail.jsp,代码如下所示:

[html] 
<%@ page language="java"contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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"> 
<meta http-equiv="refresh"content="3;url=login.jsp"> 
<title>登录失败</title> 
</head> 
<body> 
    用户名或密码错误,3秒后自动返回登陆页面... 
</body> 
</html> 


 
        接着新建一个servlet文件LoginServlet.Java,注意此init()方法中强制获取bean的方法:

[java]
package com.ldfsoft.servlet; 
  
import java.io.IOException; 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException;&

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,