Spring与Struts2.x整合的最小配置

1.需要导入的jar包
struts2-core-2.1.8.1.jar
xwork-core-2.1.8.1.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
commons-logging-1.0.4.jar
struts2-spring-plugin-2.1.8.jar//主要用来整合spring
以上均为struts2.x中的包
spring-beans.jar
spring-context.jar
spring-core.jar
spring-wen.jar
以上4个为spring中的包
2.修改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">

<!--配置Struts2-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>

<filter-mapping><!-- 过滤的映射,就是说明要过滤的东西 -->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加spring的监听器,这样可以使struts2.x的过滤器可以找到spring的bean -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--指定spring的配置文件位置,这个配置文件名可以自定义,当然要与实际的对应  -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3.写一个jsp登录界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %><!-- 引入标签库 -->
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %><!-- 日历控件 -->
<html>
<head>
<title>系统登录</title>


</head>
<body>
<table align="center" borderColor=#000000 border="1" cellpadding="2" style="border-collapse:collapse" cellspacing="0">
<tr>
<td align="center">
用户登录
</td>

</tr>
<tr>
<td >
<s:form id="id" action="loginAction"><!-- 类似于 -->
    <s:textfield name="username" label="用户名"/>
    <s:password name="password" label="密码"/>
     <s:submit value="登录"/>
   
</s:form>
</td>
</tr>
</table>
</body>
</html>
4:定义一个用户登录的接口
package com.kewen.service;

public interface ILoginService {
public boolean doLogin(String username,String password);

}
主要用于对用户名,密码的验证
5:定义这个接口的实现类:
package com.kewen.service.imp;

import com.kewen.service.ILoginService;

public class LoginService implements ILoginService {

public boolean doLogin(String username, String password) {
// TODO Auto-generated method stub
if (username.equals("skewen") && password.equals("123")) {
return true;
} else {
return false;
}

}

}
具体对用户名,密码的判断,在这里没有用到数据库,只是模拟测试
6:定义用户登录的Action类:LoginAction
package com.kewen.action;

import com.kewen.service.imp.LoginService;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction  extends ActionSupport{
/**
* @author kewen
* @date 2010-5-12
* @address ChengDu
*/
private static final long serialVersionUID = 1L;
private LoginService loginService;
private String username;
private String password;
private String  msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getUsername() {
return username;
}
//其中setter方法中的名字必须与表单控件中的名字一致
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
   public String execute() throws Exception{
   if(loginService.doLogin(username, password)){
   setMsg("登录成功!");
   return  SUCCESS;
 
   }else {
   setMsg("用户名或密码错误!");
   return INPUT;
 
   }
 
 
 
  }
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
}
我们可以看到这个类中多了一个属性loginService,这个属性我们会用spring的注入实现实例化,我们不再使用new 关键字进行实例化了
7:配置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.5.xsd">
<bean id="loginService"
class="com.kewen.service.imp.LoginService">
</bean>
<bean id="loginAction" class="com.kewen.action.LoginAction">
<property name="loginService">
<ref bean="loginService" />
</property>
</bean>

</beans>
这里就是简单的bean配置,我们可以看到对LoginAction中的属性loginService进行bean注入

8: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>

<constant name="struts.objectFactory" value="spring"></constant>
<package name="userLogin" extends='struts-default'>

<action name="loginAction" class="loginAction">
<result name="success">/login/result.jsp</result>
<result name="input">/login/Login.jsp</result>

</action>
<action name="LoginServlet" class="loginServlet.LoginServlet">
<result name="success">error.jsp</result>

</action>
</package>

    <!-- 在action比较多时可以将其写在外边,然后现引过来 -->

<!--<include file="loginXml/login.xml"></include> 
  -->
</struts>

这个文件中<constant name="struts.objectFactory"value="spring"></constant>
表示将struts中的类全部交给spring来管理

9:下面我们来运行一下吧

Spring与Struts2.x整合的最小配置


Spring与Struts2.x整合的最小配置

你可能感兴趣的:(spring,jsp,bean,struts,配置管理)