ssh 框架整合之struts-spring第一种解决方案
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- context-param -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="userForm" type="cn.bjsxt.usermgr.forms.UserForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/logininput"
forward="/login.jsp"
></action>
<action path="/login"
name="userForm"
type="cn.bjsxt.usermgr.actions.UserAction"
scope="request"
>
<forward name="sucess" path="/sucess.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
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: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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean name="userManage" class="cn.bjsxt.usermgr.impl.UserManageImpl"></bean>
</beans>
index.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>首页</title>
</head>
<body>
<a href="logininput.do">登录</a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1>登录</h1><hr/>
<form action="login.do" method="post">
姓名:<input type="text" name="username"/><br/>
秘密:<input type="password" name="password"/><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
sucess.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
${userForm.username },成功登录!!!
</body>
</html>
package cn.bjsxt.usermgr.forms;
import org.apache.struts.action.ActionForm;
public class UserForm extends ActionForm {
private String username;
private String password;
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;
}
}
package cn.bjsxt.usermgr.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.bjsxt.usermgr.UserManage;
import cn.bjsxt.usermgr.forms.UserForm;
import cn.bjsxt.usermgr.impl.UserManageImpl;
public class UserAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm uf= (UserForm)form;
// UserManage um=new UserManageImpl();
// ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
// UserManage um= (UserManage) ac.getBean("userManage");
// BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
// UserManage um= (UserManage) factory.getBean("userManage");
BeanFactory factory=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
UserManage um= (UserManage) factory.getBean("userManage");
boolean result= um.login(uf.getUsername(), uf.getPassword());
if(result){
return mapping.findForward("sucess");
}
return mapping.findForward("error");
}
}
package cn.bjsxt.usermgr;
public interface UserManage {
public boolean login(String username,String password);
}
package cn.bjsxt.usermgr.impl;
import cn.bjsxt.usermgr.UserManage;
public class UserManageImpl implements UserManage {
public boolean login(String username, String password) {
boolean isresult = false;
if (username.trim() != null && password.trim() != null) {
if (username.trim().equals("jiangao") && password.equals("123")) {
isresult = true;
} else {
isresult = false;
}
} else {
isresult = false;
}
return isresult;
}
}