三、建立对应的action
1.提供用户请求服务的LoginService类
package org.topCSA.s2s.service;
import org.topCSA.s2s.exception.PasswordException;
import org.topCSA.s2s.exception.UsernameException;
public class LoginService
{
/**//*
* 我们这只是一个小的例子,不与数据库打交到
* 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作
*/
public boolean validate(String username,String password)throws Exception
{
boolean v = false;
if(!"xcp".equals(username))//如果用户名不等于xcp,就抛出一个异常
{
throw new UsernameException("用户名不正确");
}
else if(!"123".equals(password))))//如果密码不等于123,就抛出一个异常
{
throw new PasswordException("密码不正确");
}
else
{
v = true;
}
return v;
}
}
2.接收用户请求的LoginAction类
package org.topCSA.s2s.action;
import org.topCSA.s2s.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
/** *//**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
/**//*
* 我们通过Spring的IOC容器注入LoginService,从而减少类之间的依赖关系
*/
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;
}
%26#64;Override
public void validate()
{
/**//*
* 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证
*/
}
%26#64;Override
public String execute() throws Exception
{
boolean result = loginService.validate(username, password);
if(result == true)
{
return SUCCESS;
}
else
{
return INPUT;
}
}
}
四、配置struts.xml与applicationContext.xml
1.配置struts.properties,为了解决中文问题,具体用法参照struts2的用法如下:里面加上struts.i18n.encoding = gb2312,当然也可以直接加到struts.xml里面写法为
2.配置struts.xml
xml version="1.0" encoding="GB2312" ?>
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="LoginAction">
<exception-mapping result="usernameInvalid" exception="org.topCSA.s2s.exception.UsernameException" />
<exception-mapping result="passwordInvalid" exception="org.topCSA.s2s.exception.PasswordException" />
<result name="success">/index.jspresult>
<result name="input">/login.jspresult>
<result name="usernameInvalid">/usernameInvalid.jspresult>
<result name="passwordInvalid">/passwordInvalid.jspresult>
action>
package>
struts>
3.配置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.0.xsd">
<bean name="loginService" class="org.topCSA.s2s.service.LoginService" />
<bean name="LoginAction" class="org.topCSA.s2s.action.LoginAction">
<property name="loginService">
<ref bean="loginService"/>
property>
bean>
beans>
1.提供用户请求服务的LoginService类
package org.topCSA.s2s.service;
import org.topCSA.s2s.exception.PasswordException;
import org.topCSA.s2s.exception.UsernameException;
public class LoginService
{
/**//*
* 我们这只是一个小的例子,不与数据库打交到
* 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作
*/
public boolean validate(String username,String password)throws Exception
{
boolean v = false;
if(!"xcp".equals(username))//如果用户名不等于xcp,就抛出一个异常
{
throw new UsernameException("用户名不正确");
}
else if(!"123".equals(password))))//如果密码不等于123,就抛出一个异常
{
throw new PasswordException("密码不正确");
}
else
{
v = true;
}
return v;
}
}
2.接收用户请求的LoginAction类
package org.topCSA.s2s.action;
import org.topCSA.s2s.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
/** *//**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
/**//*
* 我们通过Spring的IOC容器注入LoginService,从而减少类之间的依赖关系
*/
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;
}
%26#64;Override
public void validate()
{
/**//*
* 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证
*/
}
%26#64;Override
public String execute() throws Exception
{
boolean result = loginService.validate(username, password);
if(result == true)
{
return SUCCESS;
}
else
{
return INPUT;
}
}
}
四、配置struts.xml与applicationContext.xml
1.配置struts.properties,为了解决中文问题,具体用法参照struts2的用法如下:里面加上struts.i18n.encoding = gb2312,当然也可以直接加到struts.xml里面写法为
2.配置struts.xml
xml version="1.0" encoding="GB2312" ?>
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="LoginAction">
<exception-mapping result="usernameInvalid" exception="org.topCSA.s2s.exception.UsernameException" />
<exception-mapping result="passwordInvalid" exception="org.topCSA.s2s.exception.PasswordException" />
<result name="success">/index.jspresult>
<result name="input">/login.jspresult>
<result name="usernameInvalid">/usernameInvalid.jspresult>
<result name="passwordInvalid">/passwordInvalid.jspresult>
action>
package>
struts>
3.配置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.0.xsd">
<bean name="loginService" class="org.topCSA.s2s.service.LoginService" />
<bean name="LoginAction" class="org.topCSA.s2s.action.LoginAction">
<property name="loginService">
<ref bean="loginService"/>
property>
bean>
beans>