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>
相关文章推荐
- Spring整合Struts2简单示例
- struts2和spring的简单整合
- ZK(7.0.1)+Spring(2.3.2)+MyBatis(2.3.2)整合的简单示例
- 整合开发Struts2,Hibernate,Spring简单框架的搭建详解
- Spring整合Hibernate简单示例
- 【SSH】Struts2(2.5.14)、Spring(5.0.3)、Hibernate(5.2.13)三大框架的整合项目入门示例
- 简单示例:Spring4 整合 单个Redis服务
- Struts2与Spring、Hibernate三者整合的过程示例
- 最近S2SH框架整合和简单测试 Struts2-2.3.14+Spring-4.0.6+Hibernate-4.3.6
- Spring整合RabbitMQ简单示例
- struts2与spring的整合简单总结和使用
- Struts2 + Spring 整合简单例子(转)
- Spring3.x_Struts2.x_Hibernate3.x整合之分页pager-taglib的简单使用
- Struts2SpringHibernate框架的简单整合1
- 最简单的 Struts2 Spring2 Hibernate3 整合 实例
- struts2 spring3 hibernate4 最简单整合
- struts2 整合 Spring开发,简单数据添加小例子
- Struts2 Spring Hibernate 的简单整合
- Spring整合Hibernate简单示例
- Struts2 Spring Hibernate 的简单整合