首先第一步:
创建web工程,添加struts2开发必需的jar包到web-inf/lib目录下面, 这是必须的一些jar包,其中
是整合spring和struts2必须的包,
第二步:
在src目录下面创建struts.xml文件比如
第三步:
增加myeclipse的spring开发能力
第四步:
改写web.xml文件
第五步写action:
package com.wj.demo;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String id;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
第六步:
在src目录下创建struts.properties文件,structs2加载的话,会调用这个文件,把struts2的action对象的生成交给spring处理,代码如下
struts.objectFactory=spring
第七步:
配置spring的文件applicationContext.xml
代码如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
第八步:配置struts.xml文件
代码如下:
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
第九步:
创建jsp页面
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
success.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
你好!
欢迎你登入成功
第十步:
部署项目,发布运行
< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjxpbWcgc3JjPQ=="" alt="\">
最后总结:结合这个小小的例子,struts2整合spring可以延伸出struts2和spring更加强大的功能,需要举一反三。