《struts2权威指南》学习笔记之struts2整合Spring
来源:互联网 发布:手机北斗导航软件 编辑:程序博客网 时间:2024/05/02 00:08
整合Spring,换句话说,也就是让spring的IOC功能为我们的struts action注入逻辑组件
首先需要加载struts2-spring-plugin-2.0.6.jar这个包,这个是关键,他可以帮我们把struts2和spring仅仅整合在一起
首先是web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bean.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>aaa</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class>
</filter>
<filter-mapping>
<filter-name>aaa</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
其次是struts2.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="lee" extends="struts-default">
<action name="login" class="loginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
最关键的是bean.xml,完成为struts2 action的注入
<!-- 定义Spring配置文件的根元素,并指定Schema信息 -->
<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="myService" class="lee.MyServiceImpl"/>
<bean id="loginAction" class="lee.LoginAction" scope="prototype">
<property name="ms" ref="myService"/>
</bean>
</beans>
LoginAction
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action ...{
private String username;
private String password;
private String tip;
private MyService ms;
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public String getTip() ...{
return tip;
}
public void setTip(String tip) ...{
this.tip = tip;
}
public String execute() throws Exception ...{
if(ms.valid(this.getUsername(), this.getPassword()))...{
return SUCCESS;
}
else...{
return ERROR;
}
}
public MyService getMs() ...{
return ms;
}
public void setMs(MyService ms) ...{
this.ms = ms;
}
}
MyService及实现
public interface MyService
...{
boolean valid(String username , String pass);
}
package lee;
public class MyServiceImpl implements MyService
...{
public boolean valid(String username , String pass)
...{ System.out.println(username);
if (username.equals("高") && pass.equals("1234") )
...{
return true;
}
return false;
}
}
struts.properties
struts.i18n.encoding=gb2312
login.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center">
<caption><h3>用户登录</h3></caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr>
<td>密 码:<input type="text" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>成功页面</title>
</head>
<body>
您已经登录!
<s:property value="username"/>
<s:property value="tip"/>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
<head>
<title>错误页面</title>
</head>
<body>
您不能登录!
</body>
</html>
- 《struts2权威指南》学习笔记之struts2整合Spring
- 《struts2权威指南》学习笔记之struts2整合jsf
- 《struts2权威指南》学习笔记之struts2之文件下载
- 《struts2权威指南》学习笔记之struts2+jsf+spring+sitemesh集成开发
- 《struts2权威指南》学习笔记之struts2文件上传
- 《struts2权威指南》学习笔记之struts2文件上传
- 《struts2权威指南》学习笔记之struts2表单标签
- 《struts2权威指南》学习笔记之struts2 ajax动态表单
- 《struts2权威指南》学习笔记之struts2 pub-sub模型
- 《struts2权威指南》学习笔记之struts2 ajax标签
- struts2学习笔记之spring整合
- 《struts2权威指南》学习笔记之struts2之Tree标签改进
- 《struts2权威指南》学习笔记之struts2 ajax标签之autocompleter
- 《struts2权威指南》学习笔记之struts2 ajax标签之tabbedPanel
- 《struts2权威指南》学习笔记之struts2文件上传--使用fileUpload拦截器
- 《struts2权威指南》学习笔记之struts2多文件上传--使用数组方式
- 《struts2权威指南》学习笔记之struts2 ajax标签s和submit
- 《struts2权威指南》学习笔记之在Freemarker中使用struts2标签
- oracle数据库jdbc连接
- Oracle中插入Date数据
- samba服务器配置(练习题)
- 新闻内容页分页的简单做法
- PB事件/函数的触发机制和触发方式
- 《struts2权威指南》学习笔记之struts2整合Spring
- 用CSS改变鼠标样式
- My collection of English phrases(1)
- 想跳槽了....
- 判断开始时间
- 第一天来SOA
- 让企业舍不得你六大绝招
- 前端应用程序实现不同数据库之间的表复制
- VS2008 新增功能