RSS

依赖查找方式实现Spring与Struts1集成

01 Jan

Project Structure:

 

LoginActionForm.java

 

package com.xavier.user.forms;

import org.apache.struts.action.ActionForm;

publicclass LoginActionForm extends ActionForm {

privatestaticfinallongserialVersionUID = 1L;

private String username;

private String password;

public String getUsername() {

returnusername;

}

publicvoid setUsername(String username) {

this.username = username;

}

public String getPassword() {

returnpassword;

}

publicvoid setPassword(String password) {

this.password = password;

}

}

 

LoginAction.java

package com.xavier.user.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.web.context.support.WebApplicationContextUtils;

import com.xavier.user.forms.LoginActionForm;

import com.xavier.user.manager.UserManager;

public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response){

LoginActionForm laf =(LoginActionForm) form;

BeanFactory factory = WebApplicationContextUtils

.getRequiredWebApplicationContext(request.getSession()

.getServletContext());

UserManager userManager =(UserManager) factory.getBean(“userManager”);

userManager.login(laf.getUsername(), laf.getPassword());

return mapping.findForward(“success”);

}

}

 

index.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Index Page</title>

</head>

<body>

<a href=“logininput.do”>LOGIN</a>

</body>

</html>

 

login.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Login Page</title>

</head>

<body>

<form action=“login.do” method=“post”>

Username: <input type=“text” name=“username”/><br/>

Password: <input type=“password” name=“password”/><br/>

<input type=“submit” value=“LOGIN”/>

</form>

</body>

</html>

 

success.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<!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=UTF-8”>

<title>Success Page</title>

</head>

<body>

${loginForm.username }, user successfully logins!

</body>

</html>

 

struts-config.xml

<?xml version=“1.0” encoding=“ISO-8859-1” ?>

<!DOCTYPE struts-config PUBLIC

“-//Apache Software Foundation//DTD Struts Configuration 1.3//EN”

http://struts.apache.org/dtds/struts-config_1_3.dtd>

<struts-config>

<form-beans>

<form-bean name=“loginForm” type=“com.xavier.user.forms.LoginActionForm”/>

</form-beans>

<action-mappings>

<action path=“/logininput” forward=“/login.jsp”/>

<action path=“/login” type=“com.xavier.user.actions.LoginAction” name=“loginForm” scope=“request”>

<forward name=“success” path=“/success.jsp”/>

</action>

</action-mappings>

<message-resources parameter=“MessageResources”/>

</struts-config>

 

web.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<web-app id=“WebApp_ID” version=“2.4”

xmlns=http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>

<display-name>Struts1Spring</display-name>

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

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

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

</web-app>

 

UserManager.java

package com.xavier.user.manager;

public interface UserManager {

public void login(String username, String password);

}

 

UserManagerAdd.java

package com.xavier.user.manager.impl;

import com.xavier.user.manager.UserManager;

public class UserManagerAdd implements UserManager {

public void login(String username, String password) {

System.out.println(“UserManagerImpl.login() — username=” + username);

}

}

 

applicationContext-beans.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<beans xmlns=http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd>

<bean id=“userManager” class=“com.xavier.user.manager.impl.UserManagerAdd”/>

</beans>

Advertisements
 
Leave a comment

Posted by on 01/01/2012 in Java Web Tips

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: