当前位置: 移动技术网 > IT编程>开发语言>Java > Spring与Struts整合之使用自动装配操作示例

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

2020年03月09日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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;
  }
}

七 测试

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网