Spring+Struts2整合
1,整合Struts2
a)导入struts的jar包
struts2-core-2.1.6.jar xwork-2.1.2.jar ognl-2.6.11.jar freemarker-2.3.13.jar commons-logging-1.1.jar commons-fileupload-1.2.1.jar
b)将struts.xml文件拷贝到src目录下
c)在web.xml里配置struts的核心过滤器
<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>
2,整个Spring
a)导入Spring的jar包 spring.jar commons-logging.jar
b)将applicationContext.xml文件拷贝到WEB-INF目录下
c)在web.xml里配置spring的易做图
<!-- 配置spring易做图 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
d)添加spring与struts2整合的插件 struts2-spring-plugin-2.1.6.jar
3,编写代码测试
登录页面login.jsp
<body>
<form action="login" method="post">
USERNAME:<input type="text" name="username" /><br>
PASSWORD:<input type="password" name="password" /><br>
<input type="submit" value="登录">
</form>
</body>
登录成功页面 success.jsp
<body>
登录成功!
</body>
LoginDao业务接口
package com.itmyhome.actioin;
inte易做图ce LoginDao {
public boolean login(String username,String password);
}
LoginDaoImpl业务接口实现
package com.itmyhome.actioin;
public class LoginDaoImpl implements LoginDao {
public boolean login(String username, String password) {
// TODO Auto-generated method stub
if("zhangsan".equals(username)&&"123".equals(password)){
return true;
}else{
return false;
}
}
}
LoginAction
package com.itmyhome.actioin;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
private LoginDao loginDao; //此DAO交给spring来处理
public String execute(){
if(loginDao.login(username, password)){
return "success";
}else{
return "input";
}
}
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;
}
public LoginDao getLoginDao() {
return loginDao;
}
public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
}
}
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="default" namespace="/" extends="struts-default">
<action name="login" class="com.itmyhome.actioin.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
applicationContext.xml配置文件
<bean id="loginDao" class="com.itmyhome.actioin.LoginDaoImpl" />
<!-- 可注释 -->
<bean id="loginAction" class="com.itmyhome.actioin.LoginAction">
<property name="loginDao" ref="loginDao" />
</bean>
部署,启动tomcat 输入"zhangsan","123" 跳转到success.jsp 整合成功
摘自 itmyhome的专栏
补充:Web开发 , Jsp ,