您的位置:首页 > 编程语言 > Java开发

struts2整合Spring2.5验证登录

2009-06-03 08:45 281 查看
login.jsp登录页面

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>

<body>
<s:form action="Login" method="post">
<s:textfield name="username" label="username"></s:textfield>

<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>

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.locale" value="en_GB"></constant>
<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>

applicationcontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="loginService" class="com.struts2.service.impl.LoginServiceImpl"></bean>
<!-- scope:prototype表示原型,利于多个用户访问 -->
<bean id="loginAction" class="com.struts2.action.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"/>
</bean>
</beans>

LoginAction.java

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;
}

}

接口 :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;
}

}

结果页面:result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<body>
username:${ requestScope.username }<br>
password:${ requestScope.password }
</body>
</html>

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">

<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>
<!-- 监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>

引入的包为:struts 2.0.14下的commons-logging-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar

struts2-core-2.0.14.jar,xwork-2.0.7.jar,以及整合spring插件的struts2-spring-plugin-2.0.14.jar

以及spring2.5.6下面的spring.jar包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: