Spring,负责对象对象创建
Struts, 用Action处理请求
Spring与Struts框架整合,
关键点:让struts框架action对象的创建,交给spring完成!
步骤1:引入jar包
spring-web相关包
spring-core相关包
struts相关包
接下来配置XML文件
bean-action.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"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- action注入 -->
<bean id="userAction" class="cn.qblank.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- user注入 -->
<bean id="user" class="cn.qblank.entity.User"></bean>
</beans>
bean-service.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"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- service注入 -->
<bean id="userService" class="cn.qblank.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
bean-dao.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"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- dao注入 -->
<bean id="userDao" class="cn.qblank.dao.UserDao" scope="singleton" lazy-init="false"></bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts_" extends="struts-default">
<action name="login" class="cn.qblank.action.UserAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
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_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 配置struts核心过滤器 -->
<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>
<!-- spring环境配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
以上spring的配置可以去看spring的api,搜索context-param,找到listtener-class
然后写对应的
UserDao类
package cn.qblank.dao;
public class UserDao {
public void save() {
System.out.println("数据库添加成功");
}
}
UserService类
package cn.qblank.service;
import cn.qblank.dao.UserDao;
public class UserService {
//IOC容器注入
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
UserAction类
public class UserAction {
private User user;
private UserService userService;
public UserService getUserService() {
return userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//获取数据
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute() {
System.out.println(user.getUserName());
userService.save();
//存储数据
Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
request.put("userInfo", user.getUserName());
System.out.println(request.get("userInfo"));
return "success";
}
}
然后编写登陆表单login.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="/Spring02/login" method="POST">
用户名: <input type="text" name="user.userName"/><br/><br/>
密 码:<input type="password" name="user.pwd"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
提交表单到action中,然后存到request的Map域中
然后在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>登陆成功</title>
</head>
<body>
<h3>Spring和Struts整合完成,欢迎登陆,${userInfo }</h3>
</body>
</html>
运行结果:
实现dao层的添加功能
使用struts的request的Map存取数据