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>

struts1与struts2的区别:
<form action="login.action" method="post">
struts1通过servlet拦截所有发送过来以do结尾的请求,交有ActionServlet控制,决定由那个Action进行处理

5、新建result.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>
	username:${requestScope.username}<br> 
	password:${requestScope.password} 
</body>
</html>

6、修改web.xml
<?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>

struts1与struts2的区别:
struts1是通过servlet做控制器
struts2这里采用Filter过滤客户端发过来的请求

7、创建loginAction.java
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";		
	}
	
}

struts1与struts2的区别:
struts1必须继承Action,覆盖execute()
struts2则不需要

8、添加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" class="com.action.loginAction">
    		<result name="success">/result.jsp</result>
    	</action>
    </package>
</struts>


struts1与struts2的区别:
struts1在项目\WEB-INF\struts-config.xml配置文件
struts2在项目\src\struts.xml配置文件


9、o了,简单的例子以完成,下面是代码重构:
使用struts2.0标签,添加校验功能
1、修改login.jsp,改为使用struts2.0标签
导入标签<%@ taglib prefix="s" 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" class="com.action.loginAction">   
                <!-- 添加出错返回的页面 -->   
               <result name="input">/login.jsp</result>            <result name="success">/result.jsp</result>   
        </action>   
    </package>   
</struts>  

struts1与struts2的区别:
struts1需要在form里添加<html:errors/>标签,来提示错误处理的信息
struts2则不需要

匹配用户名为hello,密码为world
1、修改loginAction.java下的execute()方法
public String execute()throws Exception{  
        if("hello".equals(this.getUsername().trim())&&"world".equals(this.getPassword().trim())){  
            return "success";     
        }  
        else{  
            this.addFieldError("username", "username or password error");//定义在username上面提示错误信息  
            return "failer";      
        }  
              
    } 

2、修改struts.xml,增加failer映射
<?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下运行

你可能感兴趣的:(java,apache,jsp,xml,struts)