1、在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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 加载struts2核心 -->
<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>
<!-- 指明spring配置文件在何处 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 加载spring配置文件applicationContext.xml -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
2、在Struts 中配置 一些业务
<?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">
<result name="success">/index.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
3、Spring中配置依赖注入
<?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="LoginService" />
<bean name="LoginAction" class="LoginAction">
<property name="loginService">
<ref bean="loginService"/>
</property>
</bean>
</beans>
4、相关的Action类
public class LoginAction extends ActionSupport {
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;
}
@Override
public void validate()
{
/*
* 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证
*/
}
@Override
public String execute() throws Exception
{
boolean result = loginService.validate(username, password);
if(result == true)
{
return SUCCESS;
}
else
{
return INPUT;
}
}
}
5、相关的Service
public class LoginService {
/*
* 我们这只是一个小的例子,不与数据库打交到
* 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作
*/
public boolean validate(String username,String password)throws Exception
{
boolean v = false;
if(!"haha".equals(username))//如果用户名不等于haha,就抛出一个异常
{
System.out.println("用户名不正确");
}
else if(!"123".equals(password))//如果密码不等于123,就抛出一个异常
{
System.out.println("密码不正确");
}
else
{
v = true;
}
return v;
}
}
6 登陆页面
<s:form name="login" action="login" method="post" >
<s:textfield name="username" label="帐号"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit></s:submit>
</s:form>
7 显示页面
<div>
<h4>欢迎你!</h4><font color="red">${username}</font>
<br/>
<h4>你登录的密码是<h4><font color="red">${password}</font>;
</div>
跟着上面的代码 可实现简单的用户登陆功能