• java学习笔记(11) —— Struts2与Spring的整合


    1、右键 项目名称 —— MyEclipse —— Add Spring Capabilities

    2、选取 Copy checked Library contents to project folder

    3、建立IService 与 Service 【Spring 同样是面向接口编程,因此需要引入IService】

    public interface ILoginService 
    {
        public boolean isLogin(String username,String password);
    }

    public class LoginService implements ILoginService {
    
        public boolean isLogin(String username, String password) {
            if("james".equals(username) && "james".equals(password))
                return true;
            else
                return false;
        }
    }

    4、Action类引入接口,并通过具体实现调用isLogin 方法

       private ILoginService loginservice;
       
    public void setLoginservice(ILoginService loginservice) {
            this.loginservice = loginservice;
        }
    public String execute() throws Exception { if(loginservice.isLogin(this.getUsername(), this.getPassword())) return SUCCESS; else { return "failer"; } }

     5、配置struts.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"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="ILoginService" class="com.test.service.impl.LoginService"></bean>
    //每有一个请求就生成一个Action的实例
    <bean id="loginAction" class="com.test.action.LoginAction" scope="prototype">
      //loginservice 是LoginAction中引用的对象名称
    <property name="loginservice"> <ref local="ILoginService"/> </property> </bean> </beans>

    6、配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
    </context-param>
    
    <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>
    
      <servlet>
        <servlet-name>UploadServlet</servlet-name>
        <servlet-class>com.test.servlet.UploadServlet</servlet-class>
      </servlet>
     
      <servlet-mapping>
        <servlet-name>UploadServlet</servlet-name>
        <url-pattern>/UploadServlet</url-pattern>
      </servlet-mapping>
      
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    </web-app>

    7、引入的jar包

  • 相关阅读:
    文献阅读报告
    Social LSTM 实现代码分析
    文献阅读报告
    对象不止是一个对象——面向对象设计与构造第四章总结暨课程总结
    当代码遇到数理逻辑——面向对象设计与构造第三章总结
    学会拒绝,是一种智慧——OO电梯章节优化框架的思考
    学会与“有生命力”的对象打交道——面向对象设计与构造第二章总结
    从结构和数字看OO——面向对象设计与构造第一章总结
    爬取漫画DB上的JoJo的奇妙冒险 第七部 飙马野郎
    爬取5家公司(如:阿里巴巴、京东、亚马逊、华为、贵州茅台)百度“资讯”新闻的10页内容
  • 原文地址:https://www.cnblogs.com/cklovefan/p/5271772.html
Copyright © 2020-2023  润新知