您的位置:首页 > 编程语言 > Java开发

Spring整合Struts2简单示例(转)

2011-11-09 10:58 405 查看

Spring整合Struts2简单示例

1.SpringIntegrateStruts2Demo项目结构:





2.LoginAction.java源代码:

view plaincopy to clipboardprint?

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;
}
}
}

view plaincopy to clipboardprint?

package com.xqh.service;  

   
public interface MyService {  

    public boolean valid(String username, String pass);  

}  

package com.xqh.service;

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


4.MyServiceImpl.java源代码:

view plaincopy to clipboardprint?

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;

}

}

view plaincopy to clipboardprint?

<?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>  

<?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配置文件:

view plaincopy to clipboardprint?

<?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>

view plaincopy to clipboardprint?

<?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>  

<?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源代码:

view plaincopy to clipboardprint?

<%@ 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>

view plaincopy to clipboardprint?

<%@ 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>  

<%@ 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源代码:

view plaincopy to clipboardprint?

<%@ 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: