<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

  <display-name>LabWebStrutsJdbc0613</display-name>

 

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/beans.config.xml</param-value>

  </context-param>

 

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

 

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

  <resource-ref>

    <res-ref-name>jdbc/xxx</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

    <res-auth>Container</res-auth>

    <res-sharing-scope>Shareable</res-sharing-scope>

  </resource-ref>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>



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

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

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

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

       <property name="jndiName" value="java:comp/env/jdbc/xxx"></property>

    </bean>

    <bean id="customerDao" class="model.dao.CustomerDAOJdbc">

       <constructor-arg ref="dataSource"></constructor-arg>

    </bean>

    <bean id="customerService" class="model.CustomerService">

       <constructor-arg ref="customerDao"></constructor-arg>

    </bean>

    <bean id="loginAction" class="controller.LoginAction" scope="prototype">

       <property name="customerService" ref="customerService"></property>

    </bean>

    <!-- 注意scope,預設是singleton,現在每處裡一次request就產生一個action物件 -->

 

    <bean id="productDao" class="model.dao.ProductDAOJdbc">

       <constructor-arg ref="dataSource"></constructor-arg>

    </bean>

    <bean id="productService" class="model.ProductService">

       <constructor-arg ref="productDao"></constructor-arg>

    </bean>

    <bean id="productAction" class="controller.ProductAction" scope="prototype">

       <property name="productService" ref="productService"></property>

    </bean>

 

</beans>



package controller;

 

import java.util.Map;

 

import javax.servlet.ServletContext;

 

import org.apache.struts2.interceptor.SessionAware;

import org.apache.struts2.util.ServletContextAware;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

 

import com.opensymphony.xwork2.Action;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.Preparable;

 

import model.CustomerBean;

import model.CustomerService;

 

public class LoginAction extends ActionSupport implements SessionAware {

    //因為直接寫在struts.xml中了,所以ServletContextAware,Preparable 拿掉

    private String username;

    private String password;

 

    private CustomerService customerService;

    public void setCustomerService(CustomerService customerService) {

       System.out.println("000000");

       //有了setterdependency做事

       this.customerService = customerService;

    }

   

    @Override

    public void validate() {

       if (username == null || username.length() == 0) {

           this.addFieldError("username",

                  this.getText("login.username.required"));

       }

       if (password == null || password.length() == 0) {

           this.addFieldError("password",

                  this.getText("login.password.required"));

       }

    }

 

    private Map<String, Object> session;

 

    @Override

    public void setSession(Map<String, Object> session) {

       this.session = session;

    }

 

 

   

   

//  struts.xml中已經修改設定,所以不要ServletContextAware了,要被Spring接管啦

//  private ServletContext application;

//  public void setServletContext(ServletContext application) {

//     this.application = application;

//  }

 

//  @Override

//  public void prepare() throws Exception {

//     WebApplicationContext context = WebApplicationContextUtils

//            .getWebApplicationContext(application);

//     customerService = (CustomerService) context.getBean("customerService");

//  }

 

    @Override

    public String execute() throws Exception {

       CustomerBean bean = customerService.login(username, password);

       if (bean == null) {

           this.addFieldError("password", this.getText("login.failed"));

           return Action.INPUT;

       } else {

           session.put("user", bean);

           return Action.SUCCESS;

       }

    }

 

    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;

    }

}



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

 

<struts>

    <constant name="struts.objectFactory" value="spring"></constant>

    <!-- struts.objectFactory的預設值改成Spring,也就是改用Spring IoC Container管理Struts物件 -->

 

    <constant name="struts.action.extension" value="controller" />

    <!-- ↑原本應該是xxx.action,但是因為不想要這樣改,想維持controller,所以在此將預設值改掉 -->

    <constant name="struts.ui.theme" value="simple" />

 

    <package name="login" namespace="/secure" extends="struts-default">

       <!--因為物件改由String控制,所以class改為beans.xml中設定的id <action name="login" class="controller.LoginAction"> -->

       <action name="login" class="loginAction">

           <result name="input">/secure/login.jsp</result>

           <result name="success" type="redirect">/index.jsp</result>

       </action>

    </package>

 

    <package name="product" namespace="/pages" extends="struts-default">

       <!-- <action name="product" class="controller.ProductAction"> -->

       <action name="product" class="productAction">

           <result name="input">/pages/product.jsp</result>

           <result name="select">/pages/display.jsp</result>

       </action>

    </package>

 

</struts>




<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.iiiedu.samuel</groupId>

    <artifactId>LabWebStrutsJdbc</artifactId>

    <packaging>war</packaging>

    <version>1.0</version>

    <name>LabWebStrutsJdbc</name>

    <url>http://maven.apache.org</url>

 

    <properties>

       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java-version>1.8</java-version>

    </properties>

 

    <dependencies>

       <dependency>

           <groupId>org.apache.struts</groupId>

           <artifactId>struts2-spring-plugin</artifactId>

           <version>2.3.28</version>

       </dependency>

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-web</artifactId>

           <version>3.0.5.RELEASE</version>

       </dependency>

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-context</artifactId>

           <version>3.0.5.RELEASE</version>

       </dependency>

       <dependency>

           <groupId>org.apache.struts</groupId>

           <artifactId>struts2-core</artifactId>

           <version>2.3.28</version>

       </dependency>

       <dependency>

           <groupId>jstl</groupId>

           <artifactId>jstl</artifactId>

           <version>1.2</version>

       </dependency>

 

       <!-- Servlet 3.1 API -->

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>javax.servlet-api</artifactId>

           <version>3.1.0</version>

           <scope>provided</scope>

       </dependency>

 

       <!-- test dependencies -->

       <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>4.8.2</version>

           <scope>test</scope>

       </dependency>

    </dependencies>

    <build>

       <finalName>${project.artifactId}-${project.version}</finalName>

       <plugins>

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-compiler-plugin</artifactId>

              <version>2.3.2</version>

              <configuration>

                  <source>${java-version}</source>

                  <target>${java-version}</target>

              </configuration>

           </plugin>

 

           <plugin>

              <groupId>org.apache.tomcat.maven</groupId>

              <artifactId>tomcat7-maven-plugin</artifactId>

              <version>2.0</version>

           </plugin>

 

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-war-plugin</artifactId>

              <version>2.3</version>

              <configuration>

                  <failOnMissingWebXml>false</failOnMissingWebXml>

              </configuration>

           </plugin>

       </plugins>

    </build>

</project>

 

arrow
arrow
    全站熱搜

    乙方 發表在 痞客邦 留言(0) 人氣()