struts2.3.7+spring3.0.5

环境:struts2.3.7+spring3.0.5

我的目录结构图如下:

struts2.3.7+spring3.0.5_第1张图片


注意一下,上面截图是分两部分来弄得,所以有些重复,在jar包的部分,注意一下就够了,总共15个jar包。

LoginAction.java

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,从而减少类之间的依赖关系
     */
    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()
    {
        /*
         * 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证
         */
    }
    
    @Override
    public String execute() throws Exception
    {
        
        boolean result = loginService.validate(username, password);
        if(result == true)
        {
            return SUCCESS;
        }
        else
        {
            return INPUT;
        }
    }
}

PasswordException.java

package org.topCSA.s2s.exception;

public class PasswordException extends Exception {

/**
*
*/
private static final long serialVersionUID = 1L;
public PasswordException(){}
public PasswordException(String message)
{
super(message);
}

}

UsernameException
package org.topCSA.s2s.exception;

public class UsernameException extends Exception {

/**
*
*/
private static final long serialVersionUID = 1L;

public UsernameException(){};
public UsernameException(String message)
{
super(message);
}

}

LoginService.java

package org.topCSA.s2s.service;

import org.topCSA.s2s.exception.PasswordException;
import org.topCSA.s2s.exception.UsernameException;

public class LoginService
{
    /*
     * 我们这只是一个小的例子,不与数据库打交到
     * 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作
     */
    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;
    }
}

struts.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.objectFactory" value="spring" />
    <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>

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>

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.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
	
     
	<welcome-file-list>
	  <welcome-file>index.action</welcome-file>
	</welcome-file-list>	
   
    <!-- 指明spring配置文件在何处 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
	

    <!-- 加载spring配置文件applicationContext.xml -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener 
        </listener-class>
        
    </listener>    
    
</web-app>

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>

login.jsp

<%@ page language="java"  pageEncoding="utf-8"%>
<%@ 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>


passwordInvalid.jsp

<%@ page language="java"  pageEncoding="utf-8"%>
<%@ 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>

usernameInvalid.jsp

<%@ 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>

我是基于这个环境来配置我的struts2.1.8.1+spring3.0.2:
struts2 所需的jar包:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
struts2-spring-plugin-2.1.8.1.jar  //整合spring所需jar包

spring3 所需的jar包
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expr
ession-3.0.2.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar

严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 问题解决 :
需要引入javassist-3.7.ga.jar

java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
缺lang包

包的问题,因为我是struts2.X+spring3.X,而我参考别人的例子是struts2.X+spring2.X的,然后我就百度struts2+spring3的,看别人是怎么加包的,然后就把我的项目的包重新导入了一遍,然后就debug,观察console,根据里面的错误来百度下,就知道自己错在哪里,缺什么包了。

你可能感兴趣的:(struts2.3.7+spring3.0.5)