我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明struts2整合spring的相关内容.
一、准备工作
1.实例分析我们在这不与数据库打交道,所有就是当用登录的时候判断用户名是否为指定值,密码是否为指定值,以及相关的异常处理、
2.为什么我们要说struts2整合spring呢?相信在家都知道,我也不用多说了....
4.在 http://struts.apache.org/download.cgi#struts212下载struts2的jar包,源码,API文档.
5.在 http://static.springframework.org/downloads/nightly/release-download.php下载不同版本的spring的jar包,源码,API文档.
6.配置开发环境:MyEclipse6.0+Eclipse3.3+JDK6.0+Tomcat6.0+Struts 2.0.11+spring2.0。
7.新建web项目,导入相应的jar包,如以下所示:
a.由于现在IDE开发工具还没有对struts2.0有很好的支持,所有我们需要手功配置,首先将我们刚下下来的struts2.0的lib里面的commons-logging-1.0.4.jar、ognl-2.6.11.jar、xwork-2.0.4.jar、freemarker-2.3.8.jar、struts2-core-2.0.11.1.jar以及struts2.0里面所需要的插件包struts2-spring-plugin-2.0.11.1.jar添加的WEB-INF/lib下面
b.通过通过IDE开发工具项目对spring2.0的支持
7.在src下建立struts.xml文件(具体的写法在后面呈现)
8.配置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">
<filter>
<filter-name>struts2filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
filter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
web-app>
二、建立前台页面
1.用户登录肯定有一个用户登录页面login.jsp,如下:
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
<head>
<title>login2title>
head>
<body>
<s:form name="login" action="login" method="post" >
<s:textfield name="username" label="帐号">s:textfield>
<s:password name="password" label="密码">s:password>
<s:submit>s:submit>
s:form>
body>
html>
2.若登录成功的index.jsp文件,如下:
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
<head>
<title>login2title>
head>
<body>
<div>
<h4>欢迎你!h4><font color="red">${username}font>
<br/>
<h4>你登录的密码是<h4><font color="red">${password}font>;
div>
body>
html>
3、用户名非法提示页面.usernameInvalid.jsp(通过el表达示得到异常信息)
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title heretitle>
head>
<body>
用户名非法:<font color="red">${exception.message}font>
body>
html>
4、密码非法提示页面.passwordInvalid.jsp(struts2标签得到异常信息);
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title heretitle>
head>
<body>
密码非法:<FONT color="red"><s:property value="exception.message"/>FONT>
body>
html>
一、准备工作
1.实例分析我们在这不与数据库打交道,所有就是当用登录的时候判断用户名是否为指定值,密码是否为指定值,以及相关的异常处理、
2.为什么我们要说struts2整合spring呢?相信在家都知道,我也不用多说了....
4.在 http://struts.apache.org/download.cgi#struts212下载struts2的jar包,源码,API文档.
5.在 http://static.springframework.org/downloads/nightly/release-download.php下载不同版本的spring的jar包,源码,API文档.
6.配置开发环境:MyEclipse6.0+Eclipse3.3+JDK6.0+Tomcat6.0+Struts 2.0.11+spring2.0。
7.新建web项目,导入相应的jar包,如以下所示:
a.由于现在IDE开发工具还没有对struts2.0有很好的支持,所有我们需要手功配置,首先将我们刚下下来的struts2.0的lib里面的commons-logging-1.0.4.jar、ognl-2.6.11.jar、xwork-2.0.4.jar、freemarker-2.3.8.jar、struts2-core-2.0.11.1.jar以及struts2.0里面所需要的插件包struts2-spring-plugin-2.0.11.1.jar添加的WEB-INF/lib下面
b.通过通过IDE开发工具项目对spring2.0的支持
7.在src下建立struts.xml文件(具体的写法在后面呈现)
8.配置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">
<filter>
<filter-name>struts2filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
filter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
web-app>
二、建立前台页面
1.用户登录肯定有一个用户登录页面login.jsp,如下:
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
<head>
<title>login2title>
head>
<body>
<s:form name="login" action="login" method="post" >
<s:textfield name="username" label="帐号">s:textfield>
<s:password name="password" label="密码">s:password>
<s:submit>s:submit>
s:form>
body>
html>
2.若登录成功的index.jsp文件,如下:
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
<head>
<title>login2title>
head>
<body>
<div>
<h4>欢迎你!h4><font color="red">${username}font>
<br/>
<h4>你登录的密码是<h4><font color="red">${password}font>;
div>
body>
html>
3、用户名非法提示页面.usernameInvalid.jsp(通过el表达示得到异常信息)
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title heretitle>
head>
<body>
用户名非法:<font color="red">${exception.message}font>
body>
html>
4、密码非法提示页面.passwordInvalid.jsp(struts2标签得到异常信息);
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title heretitle>
head>
<body>
密码非法:<FONT color="red"><s:property value="exception.message"/>FONT>
body>
html>