struts2整合spring
来源:互联网 发布:淘宝账号和密码全忘记 编辑:程序博客网 时间:2024/06/14 19:01
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>
<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>
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://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
0 0
- Spring -- spring整合struts2
- Spring:Spring整合Struts2
- 【Spring】Spring整合Struts2
- EJB3,Spring,Struts2整合
- struts2+hibernate+spring整合
- Struts2 与 Spring 整合
- struts2 spring hibernate 整合
- Spring struts2 hibernate 整合
- struts2整合spring步骤
- spring/struts2整合
- spring 整合struts2
- spring + struts2 整合
- Struts2与Spring整合
- Struts2和Spring整合
- struts2+spring+hibernate整合
- Struts2+Spring整合
- struts2 spring整合注意事项。
- Struts2+Spring整合
- 系统框架 springmvc mybatis Bootstrap html5 shiro maven
- WebStorm快捷键
- PAT (Top Level) Practise 1006 Tree Traversals - Hard Version (35)
- 单例模式的三种常用的形式
- Android: 解决动画完成后位置恢复到初始位置的问题
- struts2整合spring
- SQL分页排序的实现
- 安卓开发中文乱码
- SqlServer 表数据统计,相同属性的汇总到一列,并用逗号隔开
- PAT (Top Level) Practise 1007 Red-black Tree (35)
- JavaScipt学习中的高阶函数
- 排序算法
- Arduino作为编程器读写BIOS、bootloader、uboot或者breed
- VNC view 连接后闪退