Struts2.011.1+Spring2.5.3 整合、测试

1. 配置struts2.0

1.1导入commons-logging-1.0.4.jar

       freemarker-2.3.10.jar

       ognl-2.6.11.jar

       struts2-core-2.1.0.jar

       xwork-2.1.0.jar

1.2struts-2.0.11/apps/struts2-blank-2.0.11/WEB-INF/classes找到    

Struts.xml配置    文件放到src目录下

       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>

 <!-- 整合struts和spring需要,可以由整合包struts.2-spring-plugin-2.0.14.jar取代-->
 <constant name="struts.objectFactory"
  value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

 <package name="struts2" extends="struts-default">
  <action name="Login" class="loginAction">
   <result name="success">/result.jsp</result>
   <result name="input">/login.jsp</result>
  </action>
 </package>

</struts>

 


2配置spring2.5

2.1导入spring.jar(spring-beans.jar  spring-context.jar  spring-core.jar   spring-web.jar)

2.2 配置applicationContext.xml

(可在spring-framework-2.0.7/samples/countries/war/WEB-INF/找到)

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
http://www.springframework.org/schema/beans"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xmlns:lang="
http://www.springframework.org/schema/lang"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

 <bean id="loginService"
  class="com.struts2.service.impl.LoginServiceImpl">
 </bean>
 
 <bean id="loginAction" class="com.struts2.action.LoginAction">
  <property name="loginService" ref="loginService"></property>
 </bean>

</beans>

 

 

3配置web.xml

       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.FilterDispatcher
  </filter-class>
 </filter>

 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!-- 启动Spring -->
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

</web-app>

result.jsp

<body>
username: $ {requestScope.username}
password: $ {requestScope.password}
</body>

 

LoginService .java

package com.struts2.service;

public interface LoginService {
 public boolean isLogin(String username,String password);

}

 

LoginServiceImpl .java

package com.struts2.service.impl;

import com.struts2.service.LoginService;

public class LoginServiceImpl implements LoginService {

 public boolean isLogin(String username, String password) {
  if("hello".equals(username) && "world".equals(password)){
   return true;
  }
  return false;
 }
}

 

 

LoginAction .jsp

package com.struts2.action;

import com.opensymphony.xwork2.ActionSupport;
import com.struts2.service.LoginService;

public class LoginAction extends ActionSupport {

 private LoginService loginService;
 private String username;
 private String password;
 
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
 @Override
 public String execute() throws Exception {
  if(loginService.isLogin(username, password)){
   return SUCCESS;
  }
  return INPUT;
 }
 public void setLoginService(LoginService loginService) {
  this.loginService = loginService;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值