Spring整合Struts2(2)

来源:互联网 发布:海南大学网络自助服务 编辑:程序博客网 时间:2024/06/05 00:17

  
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>登录页面</title>
</head>
<body>
<h3>用户登录</h3>
<s:form action="loginPro">
 <s:textfield name="username" label="用户名"/>
 <s:textfield name="password" label="密码"/>
 <tr align="center">
  <td colspan="2">
  <s:submit value="登录" theme="simple"/>
  <s:reset value="重设" theme="simple"/>
  </td>
 </tr>
</s:form>
</body>
</html>

 

<?xml version="1.0" encoding="GBK"?>
<web-app 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_3_0.xsd" version="3.0">
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 
 <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>
</web-app>

 

在web-inf

下有一个applicationContext.xml

 

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 <!-- 定义一个业务逻辑组件,实现类为MyServiceImp -->
 <bean id="myService"
  class="org.crazyit.app.service.impl.MyServiceImpl"/>
 <!-- 让Spring管理的Action实例 -->
 <bean id="loginAction" class="org.crazyit.app.action.LoginAction"
  scope="prototype">
  <!-- 依赖注入业务逻辑组件 -->
  <property name="ms" ref="myService"/>
 </bean>
</beans>

 

 

struts.xml

 

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
 "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts 2配置文件的根元素 -->
<struts>
 <!-- 配置了系列常量 -->
 <constant name="struts.i18n.encoding" value="GBK"/> 
 <constant name="struts.devMode" value="true"/> 
 <package name="lee" extends="struts-default">
  <!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类
   , 而是Spring容器中的Bean实例-->
  <action name="loginPro" class="loginAction">
   <!-- 为两个逻辑视图配置视图页面 -->
   <result name="error">/WEB-INF/content/error.jsp</result>
   <result name="success">/WEB-INF/content/welcome.jsp</result>
  </action>
  <!-- 让用户直接访问该应用时列出所有视图页面 -->
  <action name="*">
   <result>/WEB-INF/content/{1}.jsp</result>
  </action>
 </package>
</struts>

 

package org.crazyit.app.action;

import com.opensymphony.xwork2.Action;

import org.crazyit.app.service.*;
 
public class LoginAction
 implements Action
{
 //下面是用于封装用户请求参数的两个属性
 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("哈哈,整合成功!");
   return SUCCESS;
  }
  else
  {
   return ERROR;
  }
 }
}

 

 

 

package org.crazyit.app.service;

 
public interface MyService
{
 boolean valid(String username , String pass);
}

 

 

package org.crazyit.app.service.impl;

import org.crazyit.app.service.*;
 
public class MyServiceImpl
 implements MyService
{
 public boolean valid(String username , String pass)
 {
  //此处只是简单示范,故直接判断用户名、密码
  //是否符合要求
  if ( username.equals("aaa")
   && pass.equals("123") )
  {
   return true;
  }
  return false;
 }
}

 

 

 

 

 

 

 

原创粉丝点击