RSS

Struts2与Spring集成

04 Jan

Project Structure:

Login.java
package com.xavier.web.action
;

import com.opensymphony.xwork2.ActionSupport;

import com.xavier.service.LoginService;

 

public class Login extends ActionSupport {

 

    private static final long serialVersionUID = 4109743220110627658L;

   

    private String username;

    private String password;

    private LoginService service;

 

    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 void setService(LoginService service) {

        this.service = service;

    }

   

    public String execute() throws Exception{

        Integer result = service.login(username, password);

        if(result == 1){

            return SUCCESS;

        }else

            return ERROR;

    }

   

}


LoginService.java
package com.xavier.service;

 

public class LoginService {

            public Integer login(String username, String password){

                        if(“xavier”.equals(username) && “123456”.equals(password)){

                                    return 1;

                        }else

                                    return 0;

            }

}

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&#8221;>

<struts>

            <package name=“default” extends=“struts-default”>

                        <action name=“login” class=“login”>

                                    <result>/success.jsp</result>

                                    <result name=“error”>/error.jsp</result>

                        </action>

            </package>

</struts>

applicationContext.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;

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

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

            xsi:schemaLocation=http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

            http://www.springframework.org/schema/aop                 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

        http://www.springframework.org/schema/tx                       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd&#8221;>

            <bean id=“loginService” class=“com.xavier.service.LoginService”/>

            <bean id=“login” class=“com.xavier.web.action.Login”>

                        <property name=“service”>

                                    <ref bean=“loginService”/>

                        </property>

            </bean>

</beans>


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

<web-app xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns=http://java.sun.com/xml/ns/javaee&#8221; xmlns:web=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&#8221; id=“WebApp_ID” version=“3.0”>

  <display-name>Struts2-Spring</display-name>

  <filter>

            <filter-name>struts2</filter-name>

            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

  </filter>

  <filter-mapping>

            <filter-name>struts2</filter-name>

            <url-pattern>/*</url-pattern>

  </filter-mapping>

  <listener>

            <listener-class>

                        org.springframework.web.context.ContextLoaderListener

            </listener-class>

  </listener>

</web-app>

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

    pageEncoding=“UTF-8”%>

<%@ taglib prefix=“s” uri=“/struts-tags” %>   

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” http://www.w3.org/TR/html4/loose.dtd&#8221;>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Login Page</title>

</head>

<body>

            <font color=“red”><s:actionerror/></font>

            <s:form action=“login”>

                        <label><s:textfield name=“username” label=“Username”/></label>

                        <label><s:textfield name=“password” label=“Password”/></label>

                        <label><s:submit value=“Submit”/><s:reset value=“Reset”/></label>

            </s: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&#8221;>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Success Page</title>

</head>

<body>

            <center>Validate successfully!</center>

</body>

</html>

error.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&#8221;>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Error Page</title>

</head>

<body>

            <center>Username or password is not correct, please re-input!</center>

</body>

</html>

Advertisements
 
Leave a comment

Posted by on 01/04/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: