采用groovy
首先,下载struts2
将其拷入web
第二,采用groovy
package com.hopeteam.struts.action;
public class LoginAction {
def username;
def password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception
{
if(getUsername().equals("fengzhizi")
&& getPassword().equals("123456"))
{
return "success";
}
else
{
return "error";
}
}
}
编写完毕,需要在struts.xml
<constant name="struts.objectFactory" value="groovyObjectFactory" />
action
<action name="Login"
class="com.hopeteam.struts.action.LoginAction.groovy">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
第三,配置完毕后将LoginAction.groovy
编写welcome.jsp:
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>success</title>
<meta. http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
用户:<s:property value="username" />登陆</br>
groovy编写struts2的action测试成功
</body>
</html>
最后,我们看一下运行结果。
欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) | Powered by Discuz! 7.2 |