struts-2.3.16.1配spring3.1

首先第一步:

                   创建web工程,添加struts2开发必需的jar包到web-inf/lib目录下面,struts-2.3.16.1配spring3.1 这是必须的一些jar包,其中struts-2.3.16.1配spring3.1是整合spring和struts2必须的包,

第二步:

       在src目录下面创建struts.xml文件比如struts-2.3.16.1配spring3.1

第三步:


        增加myeclipse的spring开发能力

第四步:

      改写web.xml文件

        struts-2.3.16.1配spring3.1

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
  <display-name>strutsandspring</display-name>
  <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>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>
  
  
</web-app>

  


第五步写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

         代码如下:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


   <bean id="loginAction" class="com.wj.demo.LoginAction"></bean>
</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">


<!-- START SNIPPET: xworkSample -->
<struts>


  
    <package name="default" extends="struts-default">
    <!-- 使用spring生成的类对象 -->
        <action name="login" class="loginAction">
             <result name="success">/success.jsp</result>
        </action>
   </package>
   
   
</struts>


第九步:

          创建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+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>login</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" method="post">
       <s:textfield name="id" label="学号:"></s:textfield>
       <s:password name="password" label="密码:"></s:password>
       <s:submit value="登入"></s:submit>
    </s:form>
  </body>
</html>



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+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>success</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>
    <h2>你好!<s:property value="id"/>欢迎你登入成功</h2>
  </body>
</html>


第十步:

    部署项目,发布运行

   struts-2.3.16.1配spring3.1



struts-2.3.16.1配spring3.1



struts-2.3.16.1配spring3.1


最后总结:结合这个小小的例子,struts2整合spring可以延伸出struts2和spring更加强大的功能,需要举一反三。



struts-2.3.16.1配spring3.1,布布扣,bubuko.com

struts-2.3.16.1配spring3.1

上一篇:《Java编程周末提高班(第一期)》开班有感 2014.03.15


下一篇:FineReport 决策报表进行控件控制,JS实现决策报表自动翻页