Spging mvc 可以針對簡單的form去處理(結合JSTL)。
web.xml
<display-name>Spring MVC Form Handling</display-name>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-config.xml
<context:component-scan base-package="com.example" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Codeless 發表在 痞客邦 留言(0) 人氣(59)
安裝環境
O.S.:Win7
JDK:1.8.0
Eclipse:Luna 4.4.1
Server:Tomcat 1.8
Codeless 發表在 痞客邦 留言(0) 人氣(496)
Spting2.5後就可以使用Annotation的部分,在使用上面(較容易)可以取代原先XML的設定,但若是遇上Annotation和XML衝突的時候,則會以XML為主。
Customer.java
package com.example ;
import org.springframework.beans.factory.annotation.Required;
public class Customer {
private String name ;
private int age ;
public String getName() {
return name;
}
@Required
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@Required
public void setAge(int age) {
this.age = age;
}
}
Main.java
Codeless 發表在 痞客邦 留言(0) 人氣(20)
Auto-wiring
config設定中autowire屬性可以依據當時的的狀況(no(預設)、byName、byType、constructor、autodetect)去自動綁定,而不需要去大量去撰寫的程式碼。
Customer.java
package com.example;
public class Customer
{
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Customer [address=" + address + "]";
}
}
Codeless 發表在 痞客邦 留言(0) 人氣(61)
注入除了一般型態外,也可針對Collection(List、Map、Set、Properties)進行注入。
Customer.java
package com.example ;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Customer {
private List nameList ;
private Map nameMap ;
private Set nameSet ;
private Properties nameProp ;
public List getNameList() {
return nameList;
}
public void setNameList(List nameList) {
this.nameList = nameList;
}
public Map getNameMap() {
return nameMap;
}
public void setNameMap(Map nameMap) {
this.nameMap = nameMap;
}
public Set getNameSet() {
return nameSet;
}
public void setNameSet(Set nameSet) {
this.nameSet = nameSet;
}
public Properties getNameProp() {
return nameProp;
}
public void setNameProp(Properties nameProp) {
this.nameProp = nameProp;
}
}
Main.java
package com.example ;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
Customer cust = (Customer) context.getBean("Customer");
System.out.println("name list:"+cust.getNameList()) ;
System.out.println("name map:"+cust.getNameMap()) ;
System.out.println("name set:"+cust.getNameSet()) ;
System.out.println("name property:"+cust.getNameProp()) ;
}
}
Codeless 發表在 痞客邦 留言(0) 人氣(8)
Codeless 發表在 痞客邦 留言(0) 人氣(38)
Codeless 發表在 痞客邦 留言(0) 人氣(5)
Codeless 發表在 痞客邦 留言(0) 人氣(13)
Codeless 發表在 痞客邦 留言(0) 人氣(22)
Spring 基本概念
Spring框架的核心就是Spring容器,主要是透過容器建立、設定、管理直到銷毀物件。容器主要是利用dependency injection (DI)去管理組成應用程式的元件(物件),而這些元件(物件)則稱為所謂的Beans。
Spring提供了下列兩種不同的容器:
Spring BeanFactory Container
HelloWorld.java
Codeless 發表在 痞客邦 留言(0) 人氣(349)