整合Struts2与Spring以及spring的自动装配

xiaoxiao2021-02-28  85

栗子:http://www.blogjava.net/xcp/archive/2008/09/13/s2s.html

一、需要的JAR文件为:Spring和Struts2框架本身需要的JAR文件以及他们所依赖的JAR文件,比如commons-logging.jar等等,另外还需要Struts2发布包中的struts2-spring-plugin-x.xx.jar。   二、整合过程:

ch20

  1-1--src---1- 各action

    1        1 -struts.xml

    1

    1--webRoot----1-各jsp页面

                  1----web-Inf----1--applicationContext.xml

                            1--web.xml     

(1)在web.xml中增加WebApplicationContext的相应配置,即listener用来自动加载spring配置文件的位置。还需配置filter 用于struts2

默认spring会加载的web-inf下的applicationContext.xml。

代码:<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--定义核心Filter FilterDispatcher --> <filter> <!-- 定义核心Filter的名称 --> <filter-name>struts2</filter-name> <!--定义核心Filter的实现类 --> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <!--核心Filter的名称 --> <filter-name>struts2</filter-name> <!--使用该核心Filter来接受所有的Web请求 --> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 初始化Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>    配置spring的配置文件的位置    Servlet 2.3及以上版本可以使用监听器,相应配置如下: <context-param>               <param-name>contextConfigLocation</param-name>               <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener>               <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

2.建立提交页jsp 如以form形式

<%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!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>login</title> </head> <body>   <center>     <s:form action="login">    <s:textfield name="uname" label="姓名:"></s:textfield>    <s:textfield name="upass" label="密码:"></s:textfield>    <s:submit value="登录"/>    </s:form>         </center> </body> </html>

3.建立对应的action其中的属性为form中的那几个。可以再写一个其他类,用于判定的条件。此处是Logincheck

package com.web.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.web.service.LoginCheck; public class LoginAction extends ActionSupport {        private static final long serialVersionUID = 1L; private String uname; private String upass; private LoginCheck lc; private String mess="input"; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public LoginCheck getLc() { return lc; } public void setLc(LoginCheck lc) { this.lc = lc; }      public String execute()throws Exception{ if(lc.check(this.getUname(), this.getUpass())) { mess="success"; ActionContext.getContext().getSession().put("uname", uname); } return mess; } }

4.上面的loginaction

package com.web.service; public class LoginCheck { public boolean check(String uname,String upass) { if(uname.equals("zhang")&&upass.equals("zhang")) { return true; } else return false; } }

5.只要是类,就需要在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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 创建业务逻辑组件实例   --> <bean id="loginCheck" class="com.web.service.LoginCheck"></bean> <!-- 创建控制器实例 --> <bean id="loginAction" class="com.web.action.LoginAction"> <property name="lc" ref="loginCheck"></property> </bean> </beans>

6.配置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="loginAction">     <result name="success">/loginSuccess.jsp</result>     <result name="input">/loginfail.jsp</result>     </action>       </package>  </struts>

7.最终将跳转的结果页建出来即可。

转载请注明原文地址: https://www.6miu.com/read-24190.html

最新回复(0)