stucts2.0简单示例
Struts2.0是struts1.0和WebWork2.2合并版本,集成了两大流行MVC框架的优点,对struts框架来说是一个大的提升,同时也更大程度地简化了开发人员的开发过程。
这个例子实现了一个简单的登陆效果,操作步骤如下:
1、首先,要从apache网站上下载struts2.0.11的完整包(http://struts.apache.org/downloads.html),解压后需要找到下列几个文件:
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
2、然后,我们就开始做第一个例子,新建web项目struts2
3、将上面列举的jar文件拷贝到项目struts2\WEB-INF\lib中,或添加自己的strust2.0用户库
4、新建login.jsp
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body><form action="login.action" method="post">username:<input type="text" name="username" /><p>password:<input type="password" name="password" /><p><input type="submit" name="Submit" value="提交" /></form></body></html>
<?xml version="1.0" encoding="UTF-8" ?><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title></head><body>username:${requestScope.username}<br> password:${requestScope.password} </body></html>
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>struts2.0</display-name><welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list><filter><filter-name>struts2.0</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2.0</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
package com.action;public class loginAction {private String username;private String 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{return "success";}}
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="struts2.0" extends="struts-default"> <action name="login" uri="/struts-tags"%>
把form表单替换为使用struts2.0标签<s:form action="login"><s:textfield name="username" label="username"></s:textfield><s:password name="password" label="password"></s:password><s:submit label="submit"></s:submit></s:form>
(使用标签可以自动换行,按钮默认右对齐)
2、修改loginAction.java
loginAction继承ActionSupport
重写Validate()验证方法@Override public void validate() { if(null==this.getUsername()||"".equals(this.getUsername().trim())){ this.addFieldError("username", "username error");//添加错误信息 } if(null==this.getPassword()||"".equals(this.getPassword().trim())){ this.addFieldError("password", "password error");//添加错误信息 } }
3、修改struts.xml,添加出错返回的页面<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2.0" extends="struts-default"> <action name="login" name="code"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2.0" extends="struts-default"> <action name="login" class="com.action.loginAction"> <result name="input">/login.jsp</result> <result name="success">/result.jsp</result> <!-- 增加failer映射 --> <result name="failer">/login.jsp</result> </action> </package> </struts>
3、全部完成,把制作好的web应用struts2拷贝到tomcat下运行