SSH框架学习【3】idea下整合Spring与Struts以及测试

目录

 

Struts框架搭建

Spring整合Struts框架

测试


Struts框架搭建

添加相应的配置

       <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24.1</version>
        </dependency>
       <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.24.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.24.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.3.24.1</version>
        </dependency>
       <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.24.1</version>
        </dependency>

web.xml配置

【1】配置Struts2过滤器。

过滤器可以过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符。

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

【2】配置Spring框架监听器

监听器的作用是监听一些事件的发生从而进行一些操作,比如监听ServletContext,HttpSession的创建,销毁,从而执行一些初始化加载配置文件的操作,当Web容器启动后,Spring的监听器会启动监听,监听是否创建ServletContext的对象,如果发生了创建ServletContext对象这个事件(当web容器启动后一定会生成一个ServletContext对象,所以监听事件一定会发生),ContextLoaderListener类会实例化并且执行初始化方法,将spring的配置文件中配置的bean注册到Spring容器中,监听的操作是读取applicationContext.xml,但是我们可以在web.xml中配置多个需要读取的配置文件,如下方所示,读取完成后所有的配置文件中的bean都会注册到spring容器中。

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

创建Action与struts.xml文件

CategoryAction.java

package com.study.action;
import com.opensymphony.xwork2.ActionSupport;
import com.study.model.CategoryEntity;
import com.study.service.CategoryService;
import org.apache.struts2.convention.annotation.*;
import org.springframework.stereotype.Controller;

@ParentPackage("struts-default")
@Namespace("/")
@Results({})
@Controller
public class CategoryAction extends ActionSupport {

    private String id;
    private CategoryEntity categoryEntity;
    private CategoryService categoryService;
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Action(value = "category_update",results = { @Result(name = "SUCCESS",location = "/index.jsp") })
    public String update(){

        System.out.println("update......");
        System.out.println(categoryService);
        System.out.println("id=="+categoryEntity.getId()+"type=="+categoryEntity.getType()+"hot=="+categoryEntity.getHot());
        categoryService.update(categoryEntity);
        return "SUCCESS";
    }
    @Action(value = "category_save",results = { @Result(name = "SUCCESS",location = "/index.jsp") })
    public String save(){

        System.out.println("save......");
        System.out.println(categoryService);
        System.out.println("id=="+categoryEntity.getId()+"type=="+categoryEntity.getType()+"hot=="+categoryEntity.getHot());
        categoryService.save(categoryEntity);
        return "SUCCESS";
    }

    public CategoryEntity getCategoryEntity() {
        return categoryEntity;
    }

    public void setCategoryEntity(CategoryEntity categoryEntity) {
        this.categoryEntity = categoryEntity;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

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>

    <!-- 请求参数的编码方式-->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 国际化资源 -->
    <constant name="struts.custom.i18n.resources" value="i18n/globalMessages"/>
    <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开-->
    <constant name="struts.action.extension" value="action"/>
    <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->
    <constant name="struts.devMode" value="true"/>
    <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 指定由spring负责action对象的创建 -->
    <constant name="struts.objectFactory" value="spring"/>
    <!-- 是否开启动态方法调用-->
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

    <!--<package name="shop" extends="struts-default">
        <action name="category_*" class="category" method="{1}">
            <result name="SUCCESS">/index.jsp</result>
        </action>
    </package>-->

</struts>

两种方式xml配置与注解配置

XML 配置方式

注解配置方式

Spring整合Struts框架

整合其实就是把Struts框架整合到Spring,让Spring进行统一的管理,具体配置

applicationContext.xml

<!--Spring默认是单例模式,这样只会创建一个Action,每次访问都是同一个Action对象,不安全-->
        <!--prototype保证每次请求访问都对于不同的Action-->
        <bean id="category" class="com.study.action.CategoryAction" scope="prototype">
            <property name="categoryService" ref="categoryService"/>
        </bean>

struts2的配置可以通过XML或者直接采用注解的方式配置

测试

index.jsp

<html>
<head>
    <h2>Hello World!</h2>
</head>
<body>

    <!-- 下面两种写法都可以访问 -->
    <a href="${pageContext.request.contextPath}/category_update.action?categoryEntity.id=1&categoryEntity.type=gga&categoryEntity.hot=1">update</a>
    <a href="${pageContext.request.contextPath}/category_save.action?categoryEntity.id=10&categoryEntity.type=gga&categoryEntity.hot=1">save</a>

</body>
</html>

结果:

遇到的错误:

No result defined for action com.study.action.CategoryAction and result input

在搭建过程中出现了这样的错误,刚开始以为是struts.xml配置错误,检查了几遍都没有错误,最后是前端jsp出的错误,修改之后就可以了。因此以后出问题之后,可以分步骤进行测试。可以先试试不传参数是否出错,然后再试试传递一个参数是否有错,再试试传递对象是否有错。逐步排查问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值