用过struts的朋友都知道,如果我们需要在jsp中加参数,必须一个一个的写上去,如果参数是从action中传过来的,我们必须先在action中把参数保存到request中,然后jsp中取得这个参数,拼凑url
现在,webwork的url参数解析功能,可以动态的生产url
我们的action
其中有一个id=123
package
ch7.example2;
import com.opensymphony.xwork.ActionSupport;
public class TestURLParem extends ActionSupport ... {
private String id;
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public String execute() throws Exception ...{
this.setId("123");
return SUCCESS;
}
}
import com.opensymphony.xwork.ActionSupport;
public class TestURLParem extends ActionSupport ... {
private String id;
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public String execute() throws Exception ...{
this.setId("123");
return SUCCESS;
}
}
我们的jsp
主要是显示传来的id数值
<%
...
@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< body >
<% ...
String id=request.getParameter("id");
out.println(id);
%>
</ body >
</ html >
pageEncoding="GB18030" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< body >
<% ...
String id=request.getParameter("id");
out.println(id);
%>
</ body >
</ html >
最重要的xwork.xml
从action跳转到页面,但是加了一个参数id=${id},我们在action中的id=123,这里的url会解析成
/ch7/param.jsp?id=123
值得注意的是,我们要想使用这个功能,必须设置type="redirect"
<
action
name
="testparam"
class
="ch7.example2.TestURLParem"
>
< result name ="success" type ="redirect" > /ch7/param.jsp?id=${id} </ result >
</ action >
< result name ="success" type ="redirect" > /ch7/param.jsp?id=${id} </ result >
</ action >