整合Struts2与Spring以及spring的自动装配
2017-06-06 21:31
501 查看
栗子:http://www.blogjava.net/xcp/archive/2008/09/13/s2s.html
一、需要的JAR文件为:Spring和Struts2框架本身需要的JAR文件以及他们所依赖的JAR文件,比如commons-logging.jar等等,另外还需要Struts2发布包中的struts2-spring-plugin-x.xx.jar。
二、整合过程:
ch20
1-1--src---1- 各action
1 1 -struts.xml
1
1--webRoot----1-各jsp页面
1----web-Inf----1--applicationContext.xml
1--web.xml
(1)在web.xml中增加WebApplicationContext的相应配置,即listener用来自动加载spring配置文件的位置。还需配置filter 用于struts2
默认spring会加载的web-inf下的applicationContext.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 FilterDispatcher -->
<filter>
<!-- 定义核心Filter的名称 -->
<filter-name>struts2</filter-name>
<!--定义核心Filter的实现类 -->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<!--核心Filter的名称 -->
<filter-name>struts2</filter-name>
<!--使用该核心Filter来接受所有的Web请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
配置spring的配置文件的位置 Servlet
2.3及以上版本可以使用监听器,相应配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.建立提交页jsp 如以form形式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!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=UTF-8">
<title>login</title>
</head>
<body>
<center>
<s:form action="login">
<s:textfield name="uname" label="姓名:"></s:textfield>
<s:textfield name="upass" label="密码:"></s:textfield>
<s:submit value="登录"/>
</s:form>
</center>
</body>
</html>
3.建立对应的action其中的属性为form中的那几个。可以再写一个其他类,用于判定的条件。此处是Logincheck
package com.web.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.web.service.LoginCheck;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String uname;
private String upass;
private LoginCheck lc;
private String mess="input";
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
public LoginCheck getLc() {
return lc;
}
public void setLc(LoginCheck lc) {
this.lc = lc;
}
public String execute()throws Exception{
if(lc.check(this.getUname(), this.getUpass()))
{
mess="success";
ActionContext.getContext().getSession().put("uname", uname);
}
return mess;
}
}
4.上面的loginaction
package com.web.service;
public class LoginCheck {
public boolean check(String uname,String upass)
{
if(uname.equals("zhang")&&upass.equals("zhang"))
{
return true;
}
else
return false;
}
}
5.只要是类,就需要在applicationContext.xml中进行配置,通过他来维护他们之间的关系
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 创建业务逻辑组件实例 -->
<bean id="loginCheck" class="com.web.service.LoginCheck"></bean>
<!-- 创建控制器实例 -->
<bean id="loginAction" class="com.web.action.LoginAction">
<property name="lc" ref="loginCheck"></property>
</bean>
</beans>
6.配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">/loginSuccess.jsp</result>
<result name="input">/loginfail.jsp</result>
</action>
</package>
</struts>
7.最终将跳转的结果页建出来即可。
一、需要的JAR文件为:Spring和Struts2框架本身需要的JAR文件以及他们所依赖的JAR文件,比如commons-logging.jar等等,另外还需要Struts2发布包中的struts2-spring-plugin-x.xx.jar。
二、整合过程:
ch20
1-1--src---1- 各action
1 1 -struts.xml
1
1--webRoot----1-各jsp页面
1----web-Inf----1--applicationContext.xml
1--web.xml
(1)在web.xml中增加WebApplicationContext的相应配置,即listener用来自动加载spring配置文件的位置。还需配置filter 用于struts2
默认spring会加载的web-inf下的applicationContext.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 FilterDispatcher -->
<filter>
<!-- 定义核心Filter的名称 -->
<filter-name>struts2</filter-name>
<!--定义核心Filter的实现类 -->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<!--核心Filter的名称 -->
<filter-name>struts2</filter-name>
<!--使用该核心Filter来接受所有的Web请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
配置spring的配置文件的位置 Servlet
2.3及以上版本可以使用监听器,相应配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.建立提交页jsp 如以form形式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!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=UTF-8">
<title>login</title>
</head>
<body>
<center>
<s:form action="login">
<s:textfield name="uname" label="姓名:"></s:textfield>
<s:textfield name="upass" label="密码:"></s:textfield>
<s:submit value="登录"/>
</s:form>
</center>
</body>
</html>
3.建立对应的action其中的属性为form中的那几个。可以再写一个其他类,用于判定的条件。此处是Logincheck
package com.web.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.web.service.LoginCheck;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String uname;
private String upass;
private LoginCheck lc;
private String mess="input";
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
public LoginCheck getLc() {
return lc;
}
public void setLc(LoginCheck lc) {
this.lc = lc;
}
public String execute()throws Exception{
if(lc.check(this.getUname(), this.getUpass()))
{
mess="success";
ActionContext.getContext().getSession().put("uname", uname);
}
return mess;
}
}
4.上面的loginaction
package com.web.service;
public class LoginCheck {
public boolean check(String uname,String upass)
{
if(uname.equals("zhang")&&upass.equals("zhang"))
{
return true;
}
else
return false;
}
}
5.只要是类,就需要在applicationContext.xml中进行配置,通过他来维护他们之间的关系
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 创建业务逻辑组件实例 -->
<bean id="loginCheck" class="com.web.service.LoginCheck"></bean>
<!-- 创建控制器实例 -->
<bean id="loginAction" class="com.web.action.LoginAction">
<property name="lc" ref="loginCheck"></property>
</bean>
</beans>
6.配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="login" class="loginAction">
<result name="success">/loginSuccess.jsp</result>
<result name="input">/loginfail.jsp</result>
</action>
</package>
</struts>
7.最终将跳转的结果页建出来即可。
相关文章推荐
- 整合Struts2与Spring以及spring的自动装配
- 整合Struts2与Spring以及spring的自动装配
- 整合Struts2与Spring以及spring的自动装配(转)
- 整合Struts2与Spring以及spring的自动装配
- struts2和spring的两种整合方式 action的自动装配
- Spring 整合Struts2 自动装配 service 注入失败
- 8.6.4: Spring整合Struts2---使用自动装配
- struts2和spring的两种整合方式 action的自动装配
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
- spring的Autowired,Qualifier注解介绍以及自动装配
- struts2与Spring集成的自动装配策略
- 【JavaWeb-25】事务管理相关知识、手动/半自动/自动管理事务案例、整合Junit、整合Web、spring和hibernate整合、struts2和spring整合
- Hibernate注解使用以及Spring整合 , hibernate 自动生成uuid类型的id 注解
- Struts2与Spring集成中的自动装配策略
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- Struts2与Spring集成中的自动装配策略
- 框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean