Spring整合Struts2简单示例

Spring整合Struts2简单示例

1.SpringIntegrateStruts2Demo项目结构:


2.LoginAction.java源代码:

package com.xqh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.xqh.service.MyService;

public class LoginAction extends ActionSupport{
	
	//下面是用于封装用户请求参数的两个属性
	private String username;
	private String password;
	//用于封装处理结果的属性
	private String tip;
	//系统所用的业务逻辑组件
	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;
	}
	//tip属性的getter和setter方法
	public void setTip(String tip)
	{
		this.tip = tip;
	}
	public String getTip()
	{
		return this.tip;
	}
	//处理用户请求的execute方法
	public String execute() throws Exception
	{
		//调用业务逻辑组件的valid方法来
		//验证用户输入的用户名和密码是否正确
		if (ms.valid(getUsername(), getPassword()))
		{
			setTip("Spring与Struts2整合成功!");
			return SUCCESS;
		}
		else
		{
			return ERROR;
		}
	}
}

3.MyService.java源代码:

package com.xqh.service;

public interface MyService {
	public boolean valid(String username, String pass);
}

4.MyServiceImpl.java源代码:

package com.xqh.service;

public class MyServiceImpl implements MyService{

	@Override
	public boolean valid(String username, String pass) {
		if (username.equals("xqh") && pass.equals("123"))
			return true;
		return false;
	}
	
}

5.struts.xml配置文件:

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
	<!-- 配置了系列常量 -->
	<constant name="struts.i18n.encoding" value="GBK"/>	
	<package name="login" extends="struts-default">
		<!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类
			, 而是Spring容器中的Bean实例-->
		<action name="login" class="loginAction">
			<!-- 为两个逻辑视图配置视图页面 -->
			<result name="error">/error.jsp</result>
			<result name="success">/welcome.jsp</result>
		</action>
		<!-- 让用户直接访问该应用时列出所有视图页面 -->
		<action name="">
			<result>.</result>
		</action>
	</package>
</struts>

6.applicationContext.xml配置文件:

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的DTD信息 -->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Spring配置文件的根元素 -->
<beans>
	<!-- 定义一个业务逻辑组件,实现类为com.xqh.service.MyServiceImpl -->
	<bean id="myService" class="com.xqh.service.MyServiceImpl"/>
	<!-- 让Spring管理的Action实例 -->
	<bean id="loginAction" class="com.xqh.action.LoginAction"
		scope="prototype">
		<!-- 依赖注入业务逻辑组件 -->
		<property name="ms" ref="myService"/>
	</bean>
</beans>

注意:当Spring容器管理Struts2的Action时,由于每个Action对应一次用户请求,且封装了该请求的请求百状态信息,所以不应该将Action配置成单例模式,因些必须指定scope属性,该属性值可指定为prototype和request两种

7.web.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">
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <!-- 使用ContextLoaderListener初始化Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 定义Struts2的FilterDispathcer的Filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<!-- FilterDispatcher用来初始化Struts2并且处理所有的WEB请求。 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

8.login.jsp源代码:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>登录页面</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>
    <form action="login.action" method="post">
    <table align="center">
    <caption><h3>用户登录</h3></caption>
        <tr>
            <td>用户名:<input type="text" name="username"/></td>
        </tr>
        <tr>
            <td>密  码:<input type="text" name="password"/></td>
        </tr>
        <tr align="center">
            <td colspan="2"><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
        </tr>
    </table>
</form>
  </body>
</html>

9.error.jsp源代码:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>错误页面</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>
   	登录失败!
  </body>
</html>

10.welcome.jsp源代码:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>成功页面</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>
   	您已经登录!<br/>
		<s:property value="tip"/>
  </body>
</html>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值