Eclipse+struts2+spring整合实现登陆功能

来源:互联网 发布:淘宝app官方下载苹果 编辑:程序博客网 时间:2024/05/09 09:17

项目目录:

架包:



1.配置web.xml文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ZLogin</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 
</web-app>

 

2.新建service包以及LoginService.java:

 

package com.struts2.service;

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

 

3.新建action包以及LoginAction.java:

 

package com.struts2.action;

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

public class LoginAction extends ActionSupport{
 private String username;
 private String password;
 private LoginService loginService;
 
 
 public LoginService getLoginService() {
  return loginService;
 }
 public void setLoginService(LoginService loginService) {
  this.loginService = loginService;
 }
 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;
 }
 
 public String execute() throws Exception{
  if(loginService.isLogin(username, password)){
   return "success";
  }
  return "input";
 }
}

 

4.新建impl包以及LoginServiceImpl.java:

 

package com.struts2.service.impl;

import com.struts2.service.LoginService;

public class LoginServiceImpl implements LoginService {

 @Override
 public boolean isLogin(String username, String password) {
  if("xzz".equals(username) && "123".equals(password)){
   return true;
  }
  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" 
        xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation=" 
            http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
            http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
            http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.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>

 

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="struts2" extends="struts-default">
  <action name="login" class="com.struts2.action.LoginAction">
   <result name="success">success.jsp</result>
   <result name="input">login.jsp</result>
  </action>
 </package>

</struts>

 

7.新建login.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
 <s:form action="login">
  <s:textfield name="username" label="username"></s:textfield>
  <s:password name="password" label="password"></s:password>
  <s:submit name="submit"></s:submit>
 </s:form> 
</body>
</html>

 

8.新建success.jsp

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
username:${requestScope.username }<br>
password:${requestScope.password }
</body>
</html>

输入:

用户名:xzz密码:123

登录成功。否则重新输入


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 看电焊后眼睛痛怎么办 眼睛焊电焊很痛怎么办 电焊没带眼睛痛怎么办 有东西进眼睛了怎么办 眼睛有东西磨眼怎么办 怀孕了眼睛肿疼怎么办 眼睛疼又红血丝怎么办 用眼过度眼睛疼怎么办 眼睛玩手机视力下降怎么办 看手机眼睛疼该怎么办 眼睛眨一下就痛怎么办 着火了怎么办教案详案 汽车尾灯磕破了怎么办 后尾灯灯罩裂了怎么办 七氟丙烷喷伤了怎么办 冒险岛2fps低怎么办 虐杀原形2很卡怎么办 玩虐杀原形2卡怎么办 虐杀原形2闪退怎么办 电脑显示不出u盘怎么办 电脑不显示u盘怎么办 u盘在电脑不显示怎么办 笔记本不识别u盘怎么办 u盘突然识别不了怎么办 xp电脑读不出u盘怎么办 电脑无法读取u盘怎么办 win7电脑不读u盘怎么办 电脑识别不出u盘怎么办 u盘电脑读不出来怎么办 u盘突然无法识别怎么办 u盘电脑无法识别怎么办 系统无法识别u盘怎么办 手机u盘无法识别怎么办 u盘无法被识别怎么办 电脑不能读取u盘怎么办 电脑装系统卡了怎么办 怀孕三个月胚胎停育怎么办 被蟑螂咬了怎么办图片 有家人进了传销怎么办 有亲人进了传销怎么办 误入传销违法了怎么办