一. JSP:
1. EL(Expression Language):
Replacing <%=>
Expression: ${}
2. JSTL(JSP Standard Tag Library):
Tag: <c:if>: <c:if test="${user.sex =='m'}" > 男 </c:if>
<c:forEach>: <table>
<tr>
<td> 姓名</td>
<td> 年龄</td>
</tr>
<c:forEach items="${list}" var=“s">
<tr>
<td>${s.name}</td>
<td>${s.age}</td>
</tr>
</c:forEach>
</table>
<c:choose>
<c:when test="${user.sex == 'm'}">男 男
</c:when>
<c:when test="${user.sex =='f'}">女 女
</c:when>
<c:otherwise>…</c:otherwise>
</c:choose>
*JSTL need to import package, and put it under path: /web-inf/lib
3. JQuery:
Replacing DOM
eg: var a = document.getElementById("ttt"); --> $("#ttt")
4. AJAX(Asynchronous Javascript And XML):
Handling and interacting in the background asynchronously.
$.ajax({
url:
data:
success:function(data){
},
});
二. Spring
The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE (Enterprise Edition) platform. ----(Wiki)
1. <bean>: the root element of Spring's *.xml, <bean> can contain many sub-<bean> elements. Every <bean> can define an instance, and usually, we must to specify two properties: id(Determine the Bean's unique identifier, which is done by the container for the Bean management, access, and dependency of the Bean.The Bean id attribute is unique in the Spring container.) class(Specify the specific implementation Class for the Bean. Notice that there is no interface. Typically, Spring use the new keyword to create an instance of the Bean, so you must provide a class name for the Bean implementation class.)
2. properties: scope、lazy-init、init-method、destroy-method
3. IOC:
applicationContext.xml:
<bean id="dog" class="com.bean.Dog" scope="prototype"></bean>
<bean id="cat" class="com.bean.Cat" init-method="init"
destroy-method="destroy"
></bean>//only in singleton(scope = "prototype")
<!--通过构造器实例化bean -->
<bean id="obj1" class="java.util.GregorianCalendar"/>
<!--通过静态工厂方法实例化bean -->
<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
<!--通过实例工厂方法实例化bean -->
<bean id="obj3" class="java.util.GregorianCalendar"/>
<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
<!-- bean的作用域 -->
<bean id="obj5" class="java.util.GregorianCalendar" scope="prototype"/>
<!-- bean的生命周期 -->
<!-- bean的延迟实例化 -->
<bean id="exampleBean" class="com.bean.ExampleBean"
init-method="init" destroy-method="destroy" lazy-init="true"/>
<!-- setter注入 -->
<bean id="computer" class="com.bean.Computer">
<property name="mainboard" value="技嘉"/>
<property name="hdd" value="希捷"/>
<property name="ram" value="金士顿"/>
</bean>
<!--构造器注入 -->
<bean id="phone" class="com.bean.MobilePhone">
<constructor-arg index="0" value="ARM"/>
<constructor-arg index="1" value="2G"/>
</bean>
<!--自动装配 -->
<bean id="student" scope="prototype" class="com.bean.Student" autowire="byType"></bean>
</beans>
TestCase.java
public class TestCase {
/**
* 实例化Spring容器
*/
@Test
public void test1() {
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
System.out.println(ctx);
}
@Test
public void test2() {
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
Dog dog = (Dog)ctx.getBean("dog");//两种方法获得bean
Dog dog1 = ctx.getBean("dog", Dog.class);
System.out.println(dog==dog1);
dog.eat();
dog1.eat();
}
/**
* 3种实例化bean的方式
*/
/* @Test
public void test2() {
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
Calendar cal1 = (Calendar) ctx.getBean("obj1");
System.out.println(cal1);
Calendar cal2 = ctx.getBean("obj2", Calendar.class);
System.out.println(cal2);
Date date = ctx.getBean("obj4", Date.class);
System.out.println(date);
}*/
/**
* bean的作用域
*/
@Test
public void test3() {
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
Calendar cal1 = (Calendar) ctx.getBean("obj5");
Calendar cal2 = (Calendar) ctx.getBean("obj5");
System.out.println(cal1==cal2);
}
/**
* 1.bean的生命周期;
* 2.bean的延迟实例化;
*/
@Test
public void test4() {
String cfg = "applicationContext.xml";
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
System.out.println("----------------");
ExampleBean bean = ctx.getBean("exampleBean", ExampleBean.class);
bean.execute();
ctx.close();
}
/**
* setter注入
*/
@Test
public void test5() throws SQLException {
String cfg = "applicationContext.xml";
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
Computer computer =
ctx.getBean("computer", Computer.class);
System.out.println(computer.getMainboard());
System.out.println(computer.getHdd());
System.out.println(computer.getRam());
}
/**
* 构造器注入
*/
@Test
public void test6() throws SQLException {
String cfg = "applicationContext.xml";
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
MobilePhone phone =
ctx.getBean("phone", MobilePhone.class);
System.out.println(phone.getCpu());
System.out.println(phone.getRam());
}
/**
* 自动装配
*/
@Test
public void test7() throws SQLException {
String cfg = "applicationContext.xml";
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
Student student =
ctx.getBean("student", Student.class);
System.out.println(student.getComputer().getMainboard());
System.out.println(student.getMobilePhone());
}
/**
* 生命周期
*/
@Test
public void test8() throws SQLException {
String cfg = "applicationContext.xml";
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
System.out.println("--------------------");
Cat c = ctx.getBean("cat",Cat.class);
c.eat();
ctx.close();
}
/**
* clone
*/
@Test
public void test9() throws SQLException {
String cfg = "applicationContext.xml";
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
System.out.println("--------------------");
Student s1 = ctx.getBean("student",Student.class);
Student s2 = ctx.getBean("student",Student.class);
System.out.println("test....." + (s1==s2));
System.out.println("test1..." + (s1.getComputer()==s2.getComputer()));
ctx.close();
}
public static void test22(){
System.out.println("ddd");
}
}
applicationContext.xml:
<!-- 注入参数值 -->
<bean id="msg" class="com.bean.MessageBean">
<property name="name">
<value>张三</value>
</property>
<property name="age" value="25"/>
<property name="computer" ref="computer"/>
<property name="langs">
<list>
<value>Java</value>
<value>php</value>
<value>.net</value>
</list>
</property>
<property name="cities">
<set>
<value>北京</value>
<value>上海</value>
<value>广州</value>
</set>
</property>
<property name="score">
<map>
<entry key="aaaa" value="80"/>
<entry key="bbbb" value="90"/>
<entry key="cccc" value="100"/>
</map>
</property>
<property name="props">
<props>
<prop key="user">ddd</prop>
<prop key="password">111111</prop>
</props>
</property>
</bean>
<!-- 声明集合bean -->
<util:list id="langList">
<value>c++</value>
<value>python</value>
</util:list>
<util:set id="citySet">
<value>重庆</value>
<value>天津</value>
</util:set>
<util:map id="scoreMap">
<entry key="ssss" value="90"/>
<entry key="mmmm" value="85"/>
</util:map>
<util:properties id="paramProp">
<prop key="user">zhangSan</prop>
<prop key="password">123456</prop>
</util:properties>
<!-- 采用引用的方式注入集合 -->
<bean id="msg2" class="com.bean.MessageBean">
<property name="langs" ref="langList"/>
<property name="cities" ref="citySet"/>
<property name="score" ref="scoreMap"/>
<property name="props" ref="paramProp"/>
</bean>
<!-- 声明Properties集合,读取const.properties参数 -->
<util:properties id="const" location="classpath:const.properties"/>
<!-- 注入表达式 -->
<bean id="demo" class="com.bean.DemoBean">
<property name="name" value="#{msg.name}"/><!-- #{} -->
<property name="lang" value="#{msg.langs[0]}"/>
<property name="score" value="#{msg.score.aaaa}"/>
<property name="pageSize" value="#{const.PAGE_SIZE}"/>
</bean>
</beans>
TestCase.java:
public class TestCase {
/**
* 测试IOC
*/
@Test
public void test0() {
//将配置文件导入到上下文中
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
//通过spring创建实例
MessageBean bean = ctx.getBean("msg", MessageBean.class);
bean.execute();
}
/**
* 注入参数值
*/
@Test
public void test1() {
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
MessageBean bean = ctx.getBean("msg", MessageBean.class);
bean.execute();
}
/**
* 采用引用的方式注入集合
*/
@Test
public void test2() {
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
MessageBean bean = ctx.getBean("msg2", MessageBean.class);
bean.execute();
}
/**
* 注入表达式
*/
@Test
public void test3() {
String cfg = "applicationContext.xml";
ApplicationContext ctx =
new ClassPathXmlApplicationContext(cfg);
DemoBean bean = ctx.getBean("demo", DemoBean.class);
bean.execute();
}
}
4. Annotation:
applicationContext.xml:
<!-- 声明Properties集合,读取const.properties参数 -->
<util:properties id="const" location="classpath:const.properties"/>
<!-- 开启注解扫描 -->
<context:component-scan base-package="com"/>
DemoBean.java:
@Component
public class DemoBean implements Serializable {
private String name;
private String lang;
private String score;
@Value("#{const.PAGE_SIZE}")
private int pageSize;
public void execute() {
System.out.println("name:" + name);
System.out.println("lang:" + lang);
System.out.println("score:" + score);
System.out.println("pageSize:" + pageSize);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
ExampleBean.java:
@Component("example")
public class ExampleBean implements Serializable {
public ExampleBean() {
System.out.println("实例化ExampleBean:" + this);
}
@PostConstruct
public void init() {
System.out.println("初始化ExampleBean");
}
@PreDestroy
public void destroy() {
System.out.println("销毁ExampleBean");
}
public void execute() {
System.out.println("执行execute方法");
}
}
*The difference between @Resource and @Autowired: @Resource find by name; @Autowired find by type, and when there exit two class tend to implement an interface, spring will cast a BeanCreationExceprion. Under such way, we need to use @Qualifier to designate the true class we want to use. Remember! the parameter in @Qualifier must be the one in @Service.