`

W ebwork 2 + spring 2 + hibernate 3

阅读更多

环境搭建

1添加jar包:webwork-2-2.6.jarlib/default/*.jar

2src目录下新建xwork.xml  webwork.properties

3web.xml添加一个servlet:

com.opensymphony.webwork.dispatcher.ServletDispatcher

具体代码:

<servlet>

<servlet-name>webwork</servlet-name>

<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>webwork</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

开发基本步骤

1Action(ActionSupport)

代码:

packagecom.yang.action;

importcom.opensymphony.xwork.ActionSupport;

publicclassWebActionextendsActionSupport{

publicStringexecute(){

//返回键值,webwork的返回值,在xwork.xml中要对应相应的值,确定跳转情况

return"success";

}

}

2修改xwork.xml(action----result---)

添加:

<xwork>

<includefile="webwork-default.xml"/>

<!--下面的extends="webwork-default"不能写错,否则会报———Thereisnoresulttypedefinedfortype'null'mappedwithname'success'的错误-->

<packagename="ceshi"extends="webwork-default">

<actionname="webtest"class="com.yang.action.WebAction">

<resultname="success">/successtest.jsp</result>

</action>

</package>

</xwork>

到此,一个基本的webwork就运行起来了

页面数据的传递:

ServletActionContext()//不用也可传数据

HttpSessionsession=ServletActionContext.getRequest().getSession();

// session.setAttribute("user",user);

例子:此例用webwork的长处,直接

Longin.jsp:

<formaction="passdata.action"method="post">

user:<inputtype="text"name="user.name">

password:<inputtype="password"name="user.password">

<inputtype="submit"value="提交">

</form>

Passdata.jsp:

<body>

passdata!<br>

user:${user.name}<br>

password:${user.password}

</body>

Xwork.xml:

<actionname="passdata"class="com.yang.action.WebAction"method="passData">

<!--

<interceptor-refname="params"></interceptor-ref>

-->

<resultname="dataSuccess">/passdata.jsp</result>

</action>

WebAction.java:

privateUseruser;

publicUsergetUser(){

returnuser;

}

publicvoidsetUser(Useruser){

this.user=user;

}

publicStringpassData(){

// HttpSession session=ServletActionContext.getRequest().getSess ion();

// session.setAttribute("user",user);

return"dataSuccess";

}

WEB:

Application,session,request

使用校验框架:

1、写一个与ACTION同名校验规则文件 比如:LoginAction-validator.Xml

<validators>

<fieldname="name">

<field-validatortype="requiredstring">

<message>name必须输入</message>

</field-validator>

</field>

<fieldname="password">

<field-validatortype="requiredstring">

<message>password必须输入</message>

</field-validator>

</field>

</validators>

2、可以写一个同名的资源文件

3、配置action加一个inputresult

<resultname="input"type="dispatcher">/login.jsp</result>

4、可在input页面用标签提示 fielderror

123

Struts1.x+spring2+hibernate3

Webwork2+spring2+hibernate3

Webwork2+spring2

环境搭建:

配置webwork2(*.jar,xwork.xml,修改web.xml)

配置spring2(*.jar,applicationContext.xml,修改web.xml)

修改webwork2的属性文件webwork.properties

Webwork.objectFactory=spring

Xwork.xml

<actionname=addStudentclass=addStudentAction>

ApplicationContext.xml

<beanid=addStudentActionclass=com.kettas.AddStduentAction>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics