java

位置:IT落伍者 >> java >> 浏览文章

Struts与Velocity的简单集成


发布日期:2022年10月18日
 
Struts与Velocity的简单集成

先当然是简单的配置webxml与strutsconfigxml文件加入必要的servlet等东西: Xml代码

<?xml version= encoding=UTF?>

<webapp xmlns= xmlns:xsi=instance version= xsi:schemaLocation=; app__xsd>

<servlet>

<servletname>velocity</servletname>

<servletclass>orgapachevelocitytoolsviewservletVelocityViewServlet</servletclass>

</servlet>

<servlet>

<servletname>action</servletname>

<servletclass>orgapachestrutsactionActionServlet</servletclass>

<initparam>

<paramname>config</paramname>

<paramvalue>/WEBINF/strutsconfigxml</paramvalue>

</initparam>

<initparam>

<paramname>debug</paramname>

<paramvalue></paramvalue>

</initparam>

<initparam>

<paramname>detail</paramname>

<paramvalue></paramvalue>

</initparam>

<loadonstartup></loadonstartup>

</servlet>

<servletmapping>

<servletname>velocity</servletname>

<urlpattern>*vm</urlpattern>

</servletmapping>

<servletmapping>

<servletname>action</servletname>

<urlpattern>*do</urlpattern>

</servletmapping>

<welcomefilelist>

<welcomefile>vms/loginvm</welcomefile>

</welcomefilelist>

</webapp>

Strutsconfigxml:

<?xml version= encoding=UTF?>

<!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

<strutsconfig>

<datasources />

<formbeans >

<formbean name=loginForm type=comjiestrutsformLoginForm />

</formbeans>

<globalexceptions />

<globalforwards >

<forward name=success path=/vms/successvm />

<forward name=fail path=/vms/failvm />

<forward name=first path=/vms/loginvm />

</globalforwards>

<actionmappings >

<action

attribute=loginForm

input=/loginvm

name=loginForm

path=/login

scope=request

type=comjiestrutsactionLoginAction />

</actionmappings>

<messageresources parameter=comjiestrutsApplicationResources />

</strutsconfig>

view plaincopy to clipboardprint?

<?xml version= encoding=UTF?>

<webapp xmlns= xmlns:xsi=instance version= xsi:schemaLocation=; app__xsd>

<servlet>

<servletname>velocity</servletname>

<servletclass>orgapachevelocitytoolsviewservletVelocityViewServlet</servletclass>

</servlet>

<servlet>

<servletname>action</servletname>

<servletclass>orgapachestrutsactionActionServlet</servletclass>

<initparam>

<paramname>config</paramname>

<paramvalue>/WEBINF/strutsconfigxml</paramvalue>

</initparam>

<initparam>

<paramname>debug</paramname>

<paramvalue></paramvalue>

</initparam>

<initparam>

<paramname>detail</paramname>

<paramvalue></paramvalue>

</initparam>

<loadonstartup></loadonstartup>

</servlet>

<servletmapping>

<servletname>velocity</servletname>

<urlpattern>*vm</urlpattern>

</servletmapping>

<servletmapping>

<servletname>action</servletname>

<urlpattern>*do</urlpattern>

</servletmapping>

<welcomefilelist>

<welcomefile>vms/loginvm</welcomefile>

</welcomefilelist>

</webapp>

Strutsconfigxml:

<?xml version= encoding=UTF?>

<!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

<strutsconfig>

<datasources />

<formbeans >

<formbean name=loginForm type=comjiestrutsformLoginForm />

</formbeans>

<globalexceptions />

<globalforwards >

<forward name=success path=/vms/successvm />

<forward name=fail path=/vms/failvm />

<forward name=first path=/vms/loginvm />

</globalforwards>

<actionmappings >

<action

attribute=loginForm

input=/loginvm

name=loginForm

path=/login

scope=request

type=comjiestrutsactionLoginAction />

</actionmappings>

<messageresources parameter=comjiestrutsApplicationResources />

</strutsconfig>

<?xml version= encoding=UTF?>

<webapp xmlns= xmlns:xsi=instance version= xsi:schemaLocation=; app__xsd>

<servlet>

<servletname>velocity</servletname>

<servletclass>orgapachevelocitytoolsviewservletVelocityViewServlet</servletclass>

</servlet>

<servlet>

<servletname>action</servletname>

<servletclass>orgapachestrutsactionActionServlet</servletclass>

<initparam>

<paramname>config</paramname>

<paramvalue>/WEBINF/strutsconfigxml</paramvalue>

</initparam>

<initparam>

<paramname>debug</paramname>

<paramvalue></paramvalue>

</initparam>

<initparam>

<paramname>detail</paramname>

<paramvalue></paramvalue>

</initparam>

<loadonstartup></loadonstartup>

</servlet>

<servletmapping>

<servletname>velocity</servletname>

<urlpattern>*vm</urlpattern>

</servletmapping>

<servletmapping>

<servletname>action</servletname>

<urlpattern>*do</urlpattern>

</servletmapping>

<welcomefilelist>

<welcomefile>vms/loginvm</welcomefile>

</welcomefilelist>

</webapp>

Strutsconfigxml:

<?xml version= encoding=UTF?>

<!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

<strutsconfig>

<datasources />

<formbeans >

<formbean name=loginForm type=comjiestrutsformLoginForm />

</formbeans>

<globalexceptions />

<globalforwards >

<forward name=success path=/vms/successvm />

<forward name=fail path=/vms/failvm />

<forward name=first path=/vms/loginvm />

</globalforwards>

<actionmappings >

<action

attribute=loginForm

input=/loginvm

name=loginForm

path=/login

scope=request

type=comjiestrutsactionLoginAction />

</actionmappings>

<messageresources parameter=comjiestrutsApplicationResources />

</strutsconfig>然后是一个用于测试的登录页面:loginvm Html代码

<html xmlns=>

<head>

<meta httpequiv=ContentType content=text/html; charset=gb />

<title>登录页面</title>

<style type=text/css>

<!

STYLE {color: #FF}

>

<!

STYLE {color: red}

>

</style>

</head>

<body>

<form id=form name=form method=post action=logindo>

<label>用户名

<input type=text name=userName />

</label>

<p>

<label>密 码

<input type=password name=password />

</label>

</p>

<p>

<label>

<input type=submit name=Submit value=提交 />

</label>

</p>

</form>

#if($isSuccess)<p class=STYLE>验证通过</p>#else #end

#if($isNull)<p class=STYLE>字段不能为空</p>#else #end

</body>

</html>

view plaincopy to clipboardprint?

<html xmlns=>

<head>

<meta httpequiv=ContentType content=text/html; charset=gb />

<title>登录页面</title>

<style type=text/css>

<!

STYLE {color: #FF}

>

<!

STYLE {color: red}

>

</style>

</head>

<body>

<form id=form name=form method=post action=logindo>

<label>用户名

<input type=text name=userName />

</label>

<p>

<label>密 码

<input type=password name=password />

</label>

</p>

<p>

<label>

<input type=submit name=Submit value=提交 />

</label>

</p>

</form>

#if($isSuccess)<p class=STYLE>验证通过</p>#else #end

#if($isNull)<p class=STYLE>字段不能为空</p>#else #end

</body>

</html>

<html xmlns=>

<head>

<meta httpequiv=ContentType content=text/html; charset=gb />

<title>登录页面</title>

<style type=text/css>

<!

STYLE {color: #FF}

>

<!

STYLE {color: red}

>

</style>

</head>

<body>

<form id=form name=form method=post action=logindo>

<label>用户名

<input type=text name=userName />

</label>

<p>

<label>密 码

<input type=password name=password />

</label>

</p>

<p>

<label>

<input type=submit name=Submit value=提交 />

</label>

</p>

</form>

#if($isSuccess)<p class=STYLE>验证通过</p>#else #end

#if($isNull)<p class=STYLE>字段不能为空</p>#else #end

</body>

</html>还有一个后台处理的LoginActionjava: Java代码

public ActionForward execute(ActionMapping mapping ActionForm form

HttpServletRequest request HttpServletResponse response) {

LoginForm loginForm = (LoginForm) form;// TODO Autogenerated method stub

String userName = loginFormgetUserName();

String password = loginFormgetPassword();

if(userNameequals(jie) && passwordequals())

{

isSuccess = true;

requestsetAttribute(isSuccess isSuccess);//集成的关键点在这里把对象加入到vm模板 return mappingfindForward(first); 里面然后可以在模板里面用 $isSuccess 来使用

}else if(userName== || password==)

{

boolean isNull = true;

requestsetAttribute(isNull isNull);

return mappingfindForward(first);

}

return mappingfindForward(fail);

}

上一篇:Spring单元测试

下一篇:Struts架构中的Session对象创建和控制