温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring中怎么利用Struts实现自动装配操作

发布时间:2021-07-26 14:22:05 来源:亿速云 阅读:110 作者:Leah 栏目:编程语言

这篇文章给大家介绍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><h4>用户登录</h4><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实现自动装配操作就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI