二、SSH整合之Spring整合Struts2

创建Action类

package blog.csdn.net.mchenys.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import blog.csdn.net.mchenys.domain.Customer;
import blog.csdn.net.mchenys.service.CustomerService;

//action
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{

	private static final long serialVersionUID = 1L;

	//使用模型驱动,封装请求参数
	private Customer customer = new Customer();
	
	@Override
	public Customer getModel() {
		return customer;
	}
	
	//提供service的set方法,让spring去注入值
	private CustomerService customerService;
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public String add() {
		//调用业务层保存
		customerService.save(customer);
		return NONE;
	}
}

创建Service层

package blog.csdn.net.mchenys.service;

import blog.csdn.net.mchenys.domain.Customer;

public interface CustomerService {
	void save(Customer c);
}

package blog.csdn.net.mchenys.service.impl;

import blog.csdn.net.mchenys.domain.Customer;
import blog.csdn.net.mchenys.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

	@Override
	public void save(Customer c) {
		System.out.println(c);
	}

}

配置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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	
	<!-- 注册action
		 Spring框架默认生成CustomerAction是单例的,而Struts2框架是多例的。所以需要配置 scope="prototype"
	 -->
	<bean id="customerAction" class="blog.csdn.net.mchenys.action.CustomerAction" scope="prototype">
		<!-- 注入属性 -->
		<property name="customerService" ref="customerService"/>
	</bean>
	
	<!-- 注册service -->
	<bean id="customerService" class="blog.csdn.net.mchenys.service.impl.CustomerServiceImpl"/>
	
</beans>

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

	<package name="customer" namespace="/customer" extends="struts-default">
		
		<!-- 由于action交给了spring管理,所以class直接填spring配置文件对应action bean的id即可 -->
		<action name="*" class="customerAction" method="{1}"/>

	</package>
</struts>

编写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>添加客户</title>
</head>
<body>

	<form action="${pageContext.request.contextPath}/customer/add.action"
		method="post">
		<table>
			<tr>
				<td>姓名:</td>
				<td><input type="text" name="name" /></td>
			</tr>
			<tr>
				<td>来源:</td>
				<td><input type="text" name="source"/></td>
			</tr>
			<tr>
				<td>级别:</td>
				<td><input type="text" name="level" /></td>
			</tr>
			<tr>
				<td>电话:</td>
				<td><input type="text" name="phone"/></td>
			</tr>
			<tr>
				<td colspan="2" align="right"><input type="submit" value="提交" /></td>
			</tr>
		</table>

	</form>
</body>
</html>

测试添加功能

在这里插入图片描述
点击提交后,查看控制台,如果Customer 对象能封装对应的值,那么整合成功。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值