关闭

Spring 整合Struts2

标签: SpringSpring整合Struts
281人阅读 评论(0) 收藏 举报
分类:
首先导入所需的jar包(15个)
1springIOC
2struts2自身包
3spring整合struts2

4struts2整合spring

graphic

2、然后配置struts2核心过滤器(web.xml)
  <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>
3、创建实体类,业务类和web页面
public class User {
          private int id;
          private String name;
}

Service:
package star.july.service;
import star.july.entity.User;
public interface IUserService {
          public void save(User u);
}


UserServiceImpl 

package star.july.service;

import java.util.List;

import star.july.entity.Student;

public class StudentServiceImpl implements IStudentService {

    public List<Student> queryAll() {
        System.out.println("调用了queryAll方法");
        return null;
    }

}

JSP页面
reg.jsp
<body>
    <h2>填写注册信息</h2>
    <form action="<s:url value='/user_get'/>" method="post">
          ID:<input type="text" name="user.id"><br>
          姓名:<input type="text" name="user.name"><br>
          <input type="submit" value="提交"/>
    </form>
  </body>

success.jsp
  <body>
          <h2>注册成功!</h2>
  </body>
4、编写一个Action类,实现逻辑
package star.july.web;
import star.july.entity.User;
import star.july.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
    
    private IUserService userService;
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    //接收参数
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public String get(){
        userService.save(user);
        System.out.println("注册成功");
        return SUCCESS;
    }
}
5、配置applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
          
          <!-- 配置一个javabean
          id: 对象的唯一标记(别名)
          class: 需要创建的对象的全名
           -->
           <bean id="userServiceID" class="star.july.service.UserServiceImpl"></bean>
          <!-- 红色加粗标记要与struts.xml配置文件进行连接-->
          <bean id="userActionID" class="star.july.web.UserAction"  scope="prototype">
                   <!-- 注入service对象 -->
                   <property name="userService" ref="userServiceID"></property>
          </bean>
          
</beans>
6、配置struts2
<?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>
          <package name="base" namespace="/" extends="struts-default">
             <!-- 其中的class是applicationContext中的bean的ID,只有这样才可以二者连接起来,红色标记-->  
                <actionname="user_*"class="userActionID"method="{1}">
                             <result >/success.jsp</result>
                   </action>
          </package>
</struts>
7、在web.xml中加载Spring的配置文件
  <!-- 启动spring -->
  <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener
          </listener-class>
  </listener>
  <!-- 配置参数,指向文件路径 -->
  <context-param>
          <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:90602次
    • 积分:2163
    • 等级:
    • 排名:第18743名
    • 原创:122篇
    • 转载:1篇
    • 译文:0篇
    • 评论:31条
    文章分类
    最新评论