spring整合struts2



spring整合struts2不需要数据库文件,所以此处没有数据库文件的导入。另外需要说明的是,test.jsp是测试代码,测试web.xml中spring生成的listener可以生成IOC容器。

从而获取IOC容器的实例Person

 

目录结构:

 

spring配置文件: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">

<bean id="person" class="com.liuchangjie.spring.struts2.beans.Person">
<property name="username" value="spring"></property>
</bean>

<bean id="personService" class="com.liuchangjie.spring.struts2.service.PersonService"></bean>


<!-- 注意:在配置Struts2的action时,需要额外配置scope属性,其值必须是prototype -->
<bean id="personAction" class="com.liuchangjie.spring.struts2.action.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
</beans>

strus2配置文件: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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">
<!-- spring整合Struts2时,在Struts2中配置的spring的Action的class需要指向IOC容器该bean的id -->
<action name="person-save" class="personAction">
<result>/success.jsp</result>
</action>
</package>

</struts>

struts2配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring-8</display-name>


<!--配置spring配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- 启动IOC容器的ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置Struts2的Filter -->
<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>
</web-app>

 

类文件:PersonAction.java

package com.liuchangjie.spring.struts2.action;

import com.liuchangjie.spring.struts2.service.PersonService;

public class PersonAction {

private PersonService personService;

public void setPersonService(PersonService personService) {
this.personService = personService;
}

public String execute() {

System.out.println("PersonAction's execute...");
personService.save();
return "success";
}
}

 

类文件:Person.java

package com.liuchangjie.spring.struts2.beans;


public class Person {


private String username;


public void setUsername(String username) {
this.username = username;
}


public void hello() {
System.out.println("Hello! My name is " + username);
}
}

 

类文件:PersonService.java

package com.liuchangjie.spring.struts2.service;

public class PersonService {


public void save() {
System.out.println("PersonService's save...");
}
}

 

JSP文件:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="person-save"> Person save</a>
</body>
</html>

 

JSP文件:success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>Success page</h4>
</body>
</html>

JSP文件:test.jsp

<%@page import="com.liuchangjie.spring.struts2.beans.Person"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page
import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//1.从application域对象中得到IOC容器的实例
ApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(application);
//2.从IOC容器中得到bean
Person person = ctx.getBean(Person.class);
//3.使用bean
person.hello();
%>
</body>
</html>

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

退役人员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值