Spring与Struts整合之使用自动装配操作示例

本文实例讲述了Spring与Struts整合之使用自动装配操作。分享给大家供大家参考,具体如下:

一 Web配置

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <!-- 使用ContextLoaderListener初始化Spring容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <!-- 定义Struts 2的FilterDispathcer的Filter -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

二 applicationContext.xml配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  <!-- 定义一个业务逻辑组件,实现类为MyServiceImp,
    此处的id必须与Action的setter方法名对应 -->
  <bean id="ms"
    class="org.crazyit.app.service.impl.MyServiceImpl"/>
</beans>

三 视图

1 loginForm.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>登录页面</title>
</head>
<body>
<h3>用户登录</h3>
<s:form action="login">
  <s:textfield name="username" label="用户名"/>
  <s:textfield name="password" label="密码"/>
  <tr align="center">
    <td colspan="2">
    <s:submit value="登录" theme="simple"/>
    <s:reset value="重设" theme="simple"/>
    </td>
  </tr>
</s:form>
</body>
</html>

2 welcome.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>成功页面</title>
</head>
<body>
  您已经登录!<br/>
  <s:actionmessage />
</body>
</html>

3 error.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>错误页面</title>
</head>
<body>
  您不能登录!
</body>
</html>

四 Struts配置

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <!-- 配置了系列常量 -->
  <constant name="struts.i18n.encoding" value="GBK"/>
  <constant name="struts.devMode" value="true"/>
  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
  <package name="lee" extends="struts-default">
    <!-- 定义处理用户请求的Action -->
    <action name="login"
      class="org.crazyit.app.action.LoginAction">
      <!-- 为两个逻辑视图配置视图页面 -->
      <result name="error">/WEB-INF/content/error.jsp</result>
      <result>/WEB-INF/content/welcome.jsp</result>
    </action>
    <!-- 让用户直接访问该应用时列出所有视图页面 -->
    <action name="*">
      <result>/WEB-INF/content/{1}.jsp</result>
    </action>
  </package>
</struts>

五 action

package org.crazyit.app.action;
import com.opensymphony.xwork2.ActionSupport;
import org.crazyit.app.service.*;
public class LoginAction extends ActionSupport
{
  // 下面是用于封装用户请求参数的两个成员变量
  private String username;
  private String password;
  // 系统所用的业务逻辑组件
  private MyService ms;
  // 设值注入业务逻辑组件所必需的setter方法
  public void setMs(MyService ms)
  {
    this.ms = ms;
  }
  // username的setter和getter方法
  public void setUsername(String username)
  {
    this.username = username;
  }
  public String getUsername()
  {
    return this.username;
  }
  // password的setter和getter方法
  public void setPassword(String password)
  {
    this.password = password;
  }
  public String getPassword()
  {
    return this.password;
  }
  // 处理用户请求的execute方法
  public String execute() throws Exception
  {
    // 调用业务逻辑组件的validLogin()方法
    // 验证用户输入的用户名和密码是否正确
    if (ms.validLogin(getUsername(),getPassword()) > 0)
    {
      addActionMessage("哈哈,整合成功!");
      return SUCCESS;
    }
    return ERROR;
  }
}

六 Service

1 接口

package org.crazyit.app.service;
public interface MyService
{
  int validLogin(String username,String pass);
}

2 实现类

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class MyServiceImpl implements MyService
{
  public int validLogin(String username,String pass)
  {
    // 此处只是简单示范,故直接判断用户名、密码是否符合要求
    if ( username.equals("crazyit.org")
      && pass.equals("leegang") )
    {
      return 99;
    }
    return -1;
  }
}

七 测试

Spring与Struts整合之使用自动装配操作示例

Spring与Struts整合之使用自动装配操作示例

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...