免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
struts2整合spring應(yīng)用實例
我們知道struts1與spring整合是靠org.springframework.web.struts.DelegatingActionProxy來實現(xiàn)的,以下通過具體一個用戶登錄實現(xiàn)來說明struts2整合spring的相關(guān)內(nèi)容.
一、準備工作
1.實例分析我們在這不與數(shù)據(jù)庫打交道,所有就是當用登錄的時候判斷用戶名是否為指定值,密碼是否為指定值,以及相關(guān)的異常處理、
2.為什么我們要說struts2整合spring呢?相信在家都知道,我也不用多說了....
4.在  http://struts.apache.org/download.cgi#struts212下載struts2的jar包,源碼,API文檔.
5.在  http://static.springframework.org/downloads/nightly/release-download.php下載不同版本的spring的jar包,源碼,API文檔.
6.配置開發(fā)環(huán)境:MyEclipse6.0+Eclipse3.3+JDK6.0+Tomcat6.0+Struts 2.0.11+spring2.0。
7.新建web項目,導(dǎo)入相應(yīng)的jar包,如以下所示:
a.由于現(xiàn)在IDE開發(fā)工具還沒有對struts2.0有很好的支持,所有我們需要手功配置,首先將我們剛下下來的struts2.0的lib里面的commons-logging-1.0.4.jar、ognl-2.6.11.jar、xwork-2.0.4.jar、freemarker-2.3.8.jar、struts2-core-2.0.11.1.jar以及struts2.0里面所需要的插件包struts2-spring-plugin-2.0.11.1.jar添加的WEB-INF/lib下面
b.通過通過IDE開發(fā)工具項目對spring2.0的支持
7.在src下建立struts.xml文件(具體的寫法在后面呈現(xiàn))
8.配置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">
    <!-- 加載struts2核心 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 指明spring配置文件在何處 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <!-- 加載spring配置文件applicationContext.xml -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>
二、建立前臺頁面
1.用戶登錄肯定有一個用戶登錄頁面login.jsp,如下:
<%@ page language="java"  pageEncoding="GB2312"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
  <head>
      <title>login2</title>
  </head>
  <body>
      <s:form name="login" action="login" method="post" >
          <s:textfield name="username" label="賬號"></s:textfield>
          <s:password name="password"  label="密碼"></s:password>
          <s:submit></s:submit>
      </s:form>
  </body>
</html>
2.若登錄成功的index.jsp文件,如下:
<%@ page language="java"  pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
  <head>
      <title>login2</title>
  </head>
  <body>
          <div>
              <h4>歡迎你!</h4><font color="red">${username}</font>
              <br/>
              <h4>你登錄的密碼是<h4><font color="red">${password}</font>;
          </div>
  </body>
</html>
3、用戶名非法提示頁面.usernameInvalid.jsp(通過el表達示得到異常信息)
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    用戶名非法:<font color="red">${exception.message}</font>
</body>
</html>
4、密碼非法提示頁面.passwordInvalid.jsp(struts2標簽得到異常信息);
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
 <%@ taglib prefix="s" uri="/struts-tags"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    密碼非法:<FONT  color="red"><s:property value="exception.message"/></FONT>
</body>
</html>
三、建立對應(yīng)的action
1.提供用戶請求服務(wù)的LoginService類
package org.topCSA.s2s.service;
import org.topCSA.s2s.exception.PasswordException;
import org.topCSA.s2s.exception.UsernameException;
public class LoginService
{
    /*
     * 我們這只是一個小的例子,不與數(shù)據(jù)庫打交到
     * 若有數(shù)據(jù)庫操作,那么在這個LoginService就是操作具體Dao類實現(xiàn)登錄的相關(guān)操作
     */
    public boolean validate(String username,String password)throws Exception
    {
        boolean v = false;
        if(!"xcp".equals(username))//如果用戶名不等于xcp,就拋出一個異常
        {
            throw new UsernameException("用戶名不正確");
        }
        else if(!"123".equals(password))))//如果密碼不等于123,就拋出一個異常
        {
            throw new PasswordException("密碼不正確");
        }
        else
        {
            v = true;
        }
        return v;
    }
}
2.接收用戶請求的LoginAction類
package org.topCSA.s2s.action;
import org.topCSA.s2s.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    /*
     * 我們通過Spring的IOC容器注入LoginService,從而減少類之間的依賴關(guān)系
     */
    private LoginService loginService;
    public LoginService getLoginService()
    {
        return loginService;
    }
    public void setLoginService(LoginService loginService)
    {
        this.loginService = loginService;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    @Override
    public void validate()
    {
        /*
         * 我們可以在這個方法類加一些輸入驗證 但是為了體現(xiàn)后面我們寫的業(yè)務(wù)邏輯方法這就不驗證
         */
    }
    @Override
    public String execute() throws Exception
    {
        boolean result = loginService.validate(username, password);
        if(result == true)
        {
            return SUCCESS;
        }
        else
        {
            return INPUT;
        }
    }
}
四、配置struts.xml與applicationContext.xml
1.配置struts.properties,為了解決中文問題,具體用法參照struts2的用法如下:里面加上struts.i18n.encoding = gb2312,當然也可以直接加到struts.xml里面寫法為<constant name="struts.i18n.encoding" value="gbk"></constant>
2.配置struts.xml
<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="struts2" extends="struts-default">
        <action name="login" class="LoginAction">
            <exception-mapping result="usernameInvalid" exception="org.topCSA.s2s.exception.UsernameException" />
            <exception-mapping result="passwordInvalid" exception="org.topCSA.s2s.exception.PasswordException" />
            <result name="success">/index.jsp</result>
            <result name="input">/login.jsp</result>
            <result name="usernameInvalid">/usernameInvalid.jsp</result>
            <result name="passwordInvalid">/passwordInvalid.jsp</result>
        </action>
    </package>
</struts>
3.配置applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean name="loginService" class="org.topCSA.s2s.service.LoginService" />
    <bean name="LoginAction" class="org.topCSA.s2s.action.LoginAction">
        <property name="loginService">
            <ref bean="loginService"/>
        </property>
    </bean>
</beans>
五、測試(全部成功)
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Struts2.1.6與Spring2.5.6框架整合
struts-2.1.8.1與spring2.5.6的整合
struts2.1.6+spring整合
Struts+Spring+Hibernate整合入門詳解
MyEclipse開發(fā)SSH(Struts+Spring+Hibernate)入門
Struts2學(xué)習(xí)筆記---Struts2環(huán)境配置及第1個Struts程序
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服