码迷,mamicode.com
首页 > 其他好文 > 详细

SSH框架搭建

时间:2015-11-19 18:37:33      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

 

一、创建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文件里作为跳转的依据
    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框架。

 

SSH框架搭建

标签:

原文地址:http://www.cnblogs.com/benbenduo/p/4978094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!