基于idea的spring与struts2整合

struts2与spring都是java EE中非常流行的框架,下面就是用idea这个非常只能的java IDE工具进行spring与struts2的整合

一,新建一个Project,勾选下图红圈的两项

基于idea的spring与struts2整合_第1张图片

idea会自动下载一部分必须的jar包,并且创建好工程文件目录

二,加入缺少的jar包,idea下载了部分,但是还是缺少一些

  • springframework.asm-XXXXXX.jar
  • springframework.beans-XXXXXX.jar
  • springframework.context-XXXXXX.jar
  • springframework.expression-XXXXXX.jar
  • springframework.web-XXXXXX.jar
    XXXXX:spring版本号

三准备工作完了,就是写程序了

先将完整的目录结构贴出来


基于idea的spring与struts2整合_第2张图片

1.下完成web.xml文件



    struts2
    
    index.jsp
    
    
    
    org.springframework.web.context.ContextLoaderListener
    
    
    
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
    struts2
    /*
    

2.然后是LoginAction

package per.summer.actions;

import com.opensymphony.xwork2.ActionSupport;
import per.summer.beansFactory.MyService;

/**
 * Created by summer on 17-4-4.
 */
public class LoginAction extends ActionSupport{
   private String username;
   private String password;
   //业务逻辑组件
   private MyService ms;

    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 MyService getMs() {
    return ms;
    }

    public void setMs(MyService ms) {
    this.ms = ms;
    }

    @Override
    public String execute() throws Exception {
    if (ms.validLogin(username, password).equals("success")) {
        return SUCCESS;
    } else {
        return ERROR;
    }
    }
}

3.业务逻辑组件(MyService)与其接口

package per.summer.beansFactory;

/**
 * Created by summer on 17-4-4.
 */
public interface MyService {
    String validLogin(String username , String pass);
}



package per.summer.beans;

import per.summer.beansFactory.MyService;

/**
 * Created by summer on 17-4-4.
 */
public class MyServiceImpl implements MyService {
    public String validLogin(String username , String pass) {
    if (username.equals("summer") &&  pass.equals("manager")) {
        return "success";
    } else {
        return "error";
    }
    }
}

4.applicationContext.xml文件。该文件需要与web.xml同一目录



    
    

5.struts.xml文件






    
    
        
    
        /WEB-INF/JSP/welcome.jsp
        /WEB-INF/JSP/error.jsp
    
    

6.index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  LoginAction: summer
  Date: 17-4-4
  Time: 下午2:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


  Login Page



  
  
  


welcome.jsp与error.jsp就不给出了,自己想咋写就咋写

四,结果

基于idea的spring与struts2整合_第3张图片

你可能感兴趣的:(基于idea的spring与struts2整合)