Struts2.3+Spring3.2的整合

Struts2.3+Spring3.2的整合

分类:struts

这两天都是一直在鼓捣Struts2.3如何整合Spring3.2以及dao层到底选用什么以及如何整合。下面就把自己这两天的一些小成果分享出来也以便自己以后在实际项目中快速搭建。

首先是Struts2.3整合Spring3.2

1、新建一个web工程(这个就不说了)

2、添加Struts2.3

把Struts开发所需的jar复制到工程的lib文件中。开发struts必须包如下:

3、创建action类

声明:整个示例只有一个简单的用户登录。

本例中的action类名为 LoginAction,该类中的代码如下:

package com.riyun.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{private static final long serialVersionUID = 1L;private String userName;private String passWord;public String getUserName() {return userName;}public String getPassWord() {return passWord;}public void setUserName(String userName) {this.userName = userName;}public void setPassWord(String passWord) {this.passWord = passWord;}public String execute() throws Exception{if(userName.equals("riyun")&&passWord.equals("123")){return SUCCESS;}else{return ERROR;}}}

至于该类为什么继承ActionSupport,相信网上讲述Struts1.X与Struts2.X的文章已经很多了,这里就不罗嗦了。

4、修改项目中的index.jsp代码,修改后的index.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head><base href="<%=basePath%>"><title>My JSP ‘index.jsp’ starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!–<link rel="stylesheet" type="text/css" href="styles.css">–> </head><body><s:form action="login"><s:textfield name="userName" label="用户名" key="user"></s:textfield><s:password name="passWord" label="密码" key="password"></s:password><s:submit key="login" value="提交"></s:submit></s:form> </body></html>

新建两个success.jsp、error.jsp页面。后面action处理结果跳转有用,如果登陆成功则跳转到success.jsp,反之则跳转到error.jsp!

5、新建一个以struts.xml为名的xml文件

文件中的代码如下:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"""> <struts><!– struts的action配置文件 –><!– 所有的action都应该放在对应的package下 –><package name="riyun" extends="struts-default"><action name="login" class="com.riyun.action.LoginAction"><!– 定义逻辑视图和物理资源之间的映射 –><result name="success">/success.jsp</result><result name="error">/error.jsp</result></action></package> </struts>

6、在web.xml中添加struts拦截器。

Web.xml的代码如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=""><!– 定义struts2的核心filter –><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><!– 让struts定义的核心filter拦截所有请求 –><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><!– 项目欢迎界面 –><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

至此Struts2.3整合成功,接下来继续整合Spring3.2。

7、导入所需spring的jar包如下;

画龙画虎难画骨,知人知面不知心。

Struts2.3+Spring3.2的整合

相关文章:

你感兴趣的文章:

标签云: