如果学习过stuts2的话,再去学习stuts1是很容易的,下面谈一下,入门级的struts1
首先先建立一个web工程,由于myeclipse集成了struts1,所以不用导入相应jar包:如下图:
一切准备好后,会发现所有的东西都被建好了,配置文件也建好了,struts-config.xml名字不同与struts2的配置文件,而且位置也不同,web.xml也自动进行了相应设置
先来说一下跟strust2功能一样的注入,看源码如下:
public class User extends ActionForm {//必须继承此类
private Integer id;
private String name;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
上面的是一个javaBean,然后配置文件中写如下代码,之后,我会把完整的代码贴上:
<form-beans >
<form-bean name="user" type="com.bjsxt.po.User" ></form-bean>
</form-beans>
下面是action代码:
public class DemoAction extends Action {//必须继承此类
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = (User) form;//这一句是一定要加的,否则无法取得注入的数据
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getPhone());
return mapping.findForward("success");//跳转格式,就这么写,请看下面对应的配置文件中的格式
}
}
下面是jsp核心代码:
<form action="demo.do">
<input name="id" />
<input name="name" />
<input name="phone" />
<input type="submit" />
</form>
下面是完整的配置文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans >
<form-bean name="user" type="com.bjsxt.po.User" ></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/demo" type="com.bjsxt.action.DemoAction" name="user" >
<forward name="success" path="/index.jsp" ></forward><!--此处为跳转,应该是请求转发-->
</action>
<action path="/user" type="com.bjsxt.action.UserAction" parameter="ccc" ></action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
上面代码 中,path就是url,type就是想当于class,注意
parameter="ccc"
这就是strust1访问action的方法,这种方式很容易让人想到jsp/servlet中的前台传过来的remark不同,对应访问不同的方法一样,
比如:
<action path="/product/proManage" type="com.supplier.action.ProductAction" name="productaddForm" parameter="method"> </action> 然后在action中有个addUser的方法,那么可以在jsp中使用 url?method=addUser这样来访问这个action中的addUser方法。
请接着看所要访问action代码如下:
public class UserAction extends DispatchAction {//必须继承此类
public ActionForward add(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
System.out.println("add..........");
return null;
}
public ActionForward del(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
System.out.println("del..........");
return null;
}
}
以上代码在配置文件中都已配置,具体可以再详细看一下,访问不同方法时,只需要在url后加上ccc=要访问的方法名,即可。
值得注意的是看一下action有四个参数,其中有两个,是与servlet中一样的,这区别于strus2的完全解耦,这样对于程序员用起来会方便一些。