<?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">
<!-- 原本的JDBC -->
<!-- <bean id="dataSource" -->
<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
<!-- <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> -->
<!-- <property name="url" -->
<!-- value="jdbc:sqlserver://localhost:1433;database=java"></property> -->
<!-- <property name="username" value="sa"></property> -->
<!-- <property name="password" value="passw0rd"></property> -->
<!-- </bean> -->
<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="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>
</beans>
<?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>
package controller;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import model.ProductBean;
import model.ProductService;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class ProductAction extends ActionSupport implements RequestAware,
Preparable, ServletContextAware {
private String prodaction;
private ProductBean bean;
private Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
private ServletContext application;
@Override
public void setServletContext(ServletContext application) {
//因為實作ServletContextAware所以必須要覆寫此方法
//藉此取得ServletContext物件
//此方法會在prepare()之前執行
this.application = application;
}
@Override
public void validate() {
if ("Insert".equals(prodaction) || "Update".equals(prodaction)
|| "Delete".equals(prodaction)) {
if (bean.getId() == 0) {
this.addFieldError("bean.id", this.getText(
"product.id.required", new String[] { prodaction }));
}
}
}
private ProductService productService;
@Override
public void prepare() throws Exception {
// 因為實作Preparable而有的方法,取代init()用
//跟servlet中的init()寫的東西一樣,只除了getWebApplicationContext(application);中的application
// This method is called to allow the action to prepare itself.
ApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(application);
// 注意application是從哪裡來的
productService = (ProductService) context.getBean("productService");
}
@Override
public String execute() throws Exception {
System.out.println("產品測試");
if (prodaction != null && prodaction.equals("Select")) {
List<ProductBean> result = productService.select(bean);
request.put("select", result);
return "select";
} else if (prodaction != null && prodaction.equals("Insert")) {
ProductBean result = productService.insert(bean);
if (result == null) {
this.addFieldError("action",
this.getText("product.insert.failed"));
} else {
request.put("insert", result);
}
return Action.INPUT;
} else if (prodaction != null && prodaction.equals("Update")) {
ProductBean result = productService.update(bean);
if (result == null) {
this.addFieldError("action",
this.getText("product.update.failed"));
} else {
request.put("update", result);
}
return Action.INPUT;
} else if (prodaction != null && prodaction.equals("Delete")) {
boolean result = productService.delete(bean);
request.put("delete", result);
return Action.INPUT;
} else {
this.addFieldError("action", this.getText("product.unknown"));
return Action.INPUT;
}
}
public ProductBean getBean() {
return bean;
}
public void setBean(ProductBean bean) {
this.bean = bean;
}
public String getProdaction() {
return prodaction;
}
public void setProdaction(String prodaction) {
this.prodaction = prodaction;
}
}
留言列表