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

struts2整合spring

2016-02-28 12:25 399 查看
1.项目整体结构



2.导入所需要的包

需要注意的是,需要导入struts2-spring-plugin-xxxx.jar,否则会导致action中注入service不成功.NullPoint异常

3.JSP页面:

index.jsp

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


loginSuccess.jsp
<div>
<h4>欢迎你!</h4><font color="red">${username}</font>
<br/>
<h4>你登录的密码是<h4><font color="red">${password}</font>;
</div>


passwordInvalid.jsp

<h1>密码错误</h1>


usernameInvalid.jsp

<h1>用户名错误</h1>


4..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="cn.spring.action.LoginAction">

<exception-mapping result="usernameInvalid" exception="cn.spring.exception.UsernameException" />
<exception-mapping result="passwordInvalid" exception="cn.spring.exception.PasswordException" />

<result name="success">/loginSuccess.jsp</result>
<result name="input">/index.jsp</result>
<result name="usernameInvalid">/usernameInvalid.jsp</result>
<result name="passwordInvalid">/passwordInvalid.jsp</result>

</action>

</package>
</struts>


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:context="http://www.springframework.org/schema/context"
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 
<bean id="loginService" class="cn.spring.service.LoginService" />

<!-- action 中注入service -->
<bean id="loginAction" class="cn.spring.action.LoginAction">
<property name="loginService" ref="loginService"/>
</bean>

</beans>


6.web.xml

<!-- 配置Spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 全局参数,默认加载web-inf目录下的配置文件(applicationContext.xml),在此处进行修改 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- 配置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>
<!-- 拦截请求和转发 -->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>


7.LoginService

package cn.spring.service;

import cn.spring.exception.PasswordException;
import cn.spring.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;
}
}


8.LoginAction

package cn.spring.action;

import cn.spring.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 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()
{

* 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证

}*/

public String execute() throws Exception{
System.out.println("............."+username+",..."+this.password) ;
boolean result = loginService.validate(username, password);

System.out.println("..."+result+"...");

if(result == true){
return SUCCESS;
}
else{
return INPUT;
}
}
}


9.UsernameException

package cn.spring.exception;

public class UsernameException extends Exception{
private static final long serialVersionUID = 1L;

public UsernameException(){};

public UsernameException(String message){
super(message);
}
}


10.PasswordException

package cn.spring.exception;

public class PasswordException extends Exception{
private static final long serialVersionUID = 1L;

public PasswordException(){}

public PasswordException(String message){
super(message);
}
}


测试:
http://localhost:8080/Struts2_Spring/index.jsp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: