一、创建struts2项目

1,新建一个web工程,一个简单的登陆项目,整体项目架构如下:

action是处理请求的类,bean是一个user的模板,service是业务处理逻辑接口,serviceImpl是业务处理接口的实现,下面附具体代码

 

 

2,将struts的核心jar包复制进项目的lib目录下,具体文件如下(webroot目录):

官方网站下载struts-2.3.24.1-all.zip,解压后在apps文件夹下找到struts2-blank.war,解压,真正的核心包好像只需要五个,这里将war包解压后WEB-INF下lib里的jar包全复制过来了

 

 

3,修改web.xml文件,配置struts的核心拦截器。

 

<?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">
  <display-name></display-name>    
  
  <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.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

4,编写action,新建一个java文件,用以处理请求。这个稍微有点复杂,因为多了一个UserForm的类,在UserForm里同样定义了getter和setter方法,但是不能直接通过UserForm的实例来获取页面传入的值。

 

package com.action;

import com.bean.UserForm;
import com.opensymphony.xwork2.ActionSupport;
import com.service.UserManager;
import com.serviceImpl.UserManagerImpl;

public class Register extends ActionSupport{
    
    /**username和password 可以直接通过getter方法获得
     * user和UserManager不是页面的元素,必须初始化后才能使用
     */
    private static final long serialVersionUID = 1L;
    
    private String username;
    private String password;
    private UserForm user;
    private UserManager userManager;
    
    //构造函数,这里可以做些初始化的事情
    public Register(){ 
        this.setUserManager( new UserManagerImpl() );
        this.setUser( new UserForm() );  // new个UserForm对象传给本地私有user
    }
    
    // execute函数的返回值会传递到struts.xml文件里作为跳转的依据--因为struts.xml文件没有method属性,默认执行execute函数
    public String execute(){
            
        user.setUsername( this.getUsername() ); // this方法获取页面传递的值,再调用setter方法传给user
        user.setPassword( this.getPassword() ); // 这里的user是UserForm对象,不能直接通过jsp获取值,需要另行设置
                
        if ( userManager.dosomething( user ) ){ // 传送过去验证登陆
            return SUCCESS;
        }else{
            return ERROR;
        }        
    }
    
    // 以下四个函数是呼应jsp页面,获取页面值用的,注意命名格式
    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;
    }
    
    // 这两个函数是UserForm对象用的
    public UserForm getUser(){
        return user;
    }    
    public void setUser( UserForm user ){
        this.user = user;
    }
    
    // 这两个是UserManager对象用的,注意UserManager是个接口,setter方法里需要传送实现它的具体类实例
    public UserManager getUserManager(){
        return userManager;
    }    
    public void setUserManager( UserManager userManager ){
        this.userManager = userManager;
    }

}

 

5,UserForm、UserManager接口和UsermanagerImpl

UserForm:

package com.bean;

public class UserForm {
    
    private String username;
    private String password;
    private String gender;
    
    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;
    }
    
    public String getGender(){
        return gender;
    }
    
    public void setGender( String gender ){
        this.gender = gender;
    }

}

UserManager:

package com.service;

import com.bean.UserForm;

public interface UserManager {
    
    public boolean dosomething(UserForm user);

}

UserManagerImpl:

package com.serviceImpl;

import com.bean.UserForm;
import com.service.UserManager;

public class UserManagerImpl implements UserManager{

    public boolean dosomething(UserForm user) {
        // TODO Auto-generated method stub
        
        // 姓名为wu密码123的用户可以登录,其他失败。后期整合hibernate后改为验证数据库中的用户
        if ( "wu".equals( user.getUsername() ) && "123".equals( user.getPassword() ) ){
            return true;
        }else{
            return false;
        }
        
    }

}

 

6,新建一个struts.xml文件,用来跳转

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <include file="struts-default.xml" />
    <package name="com" extends="struts-default">
        <action name="register" class="com.action.Register">
            <result name="success">/Success.jsp</result>
            <result name="error">/Fail.jsp</result>
        </action>
        <action name="fail">
            <result>/index.jsp</result>
        </action>
    </package>

</struts>    

 

7,index.jsp页面:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    This is my JSP page. <br>
    
    <s:form action="register">
        <s:textfield name="username" label="用户名"></s:textfield>
        <s:password name="password" label="密码"></s:password>
        <s:submit values="注册"></s:submit>
    </s:form>
    
  </body>
</html>

 

    到这里struts2部分搭建完毕,下一步准备整合spring框架。

----------------------------------------------------------------------------------------------------------------------

    二、整合spring框架

 

1,导入spring的jar包,同样是官方下载3.2版本,下载后所有jar包导入lib目录,如下:

 

 

 

 

2,修改web.xml文件,其实就是在struts的基础上加上初始化spring的控制文件,配置spring监听器,当然还可以配置其他的参数,比如修改applicationContext.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">
  <display-name></display-name>    
  
   <!-- 初始化 -->
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <!-- 如果有多个文件,在文件之间用英文逗号隔开 -->
       <!-- <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-db.xml
       </param-value> -->
       <param-value>/WEB-INF/applicationContext.xml</param-value>
     </context-param>
    <!-- 监听器 -->
  <!-- 配置spring监听器 -->  
  <listener>  
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>   
  
  <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.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

3,在WEB-INF目录下新建applicationContext.xml,这个地方是默认的目录,和web.xml是同一个目录级。

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

    <!-- 这个id是struts.xml里面的action的class名,这里的class指向的是具体的action类 -->
    <bean id="loginBean" class="com.action.LoginAction">  
    </bean>  
    
    <bean id="registerBean" class="com.action.RegisterAction">
    </bean>
    
    <bean id="failBean" class="com.action.RegisterAction"></bean>
    
</beans>

 

4,修改struts.xml文件。增加一行将action托管给spring的代码,另外将action里的class修改为spring配置文件里的bean id。

 

<?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>

    <!-- 将action托管给spring -->
    <constant name="struts.objectFactory" value="spring"></constant>  
    
    <include file="struts-default.xml"></include>
    <package name="p1" extends="struts-default">
    
        <!-- 这里的name是jsp页面上配置的action, class是applicationContext.xml里的bean id,method是具体实现类里应该调用哪个方法 -->
        <action name="loginAction" class="loginBean" method="login">
            <result name="success">/Success.jsp</result>
            <result name="error">/Fail.jsp</result>
        </action>
        <action name="registerAction" class="registerBean" method="register">
            <result name="success">/register.jsp</result>
            <result name="error">/Fail.jsp</result>
        </action>
        <action name="fail" class="failBean" method="donothing">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>       

 

    配置完毕。从这个简单的例子里能看出来,原本struts直接指向具体类,修改成了指向spring,然后由spring来分配。这里没有涉及反转控制、注入之类的。

接下来整合hibernate。

---------------------------------------------------------------------------------------------------------------------------------------------------

 

posted on 2015-11-19 16:41  笨笨多  阅读(267)  评论(0编辑  收藏  举报