2007-12-10

struts2启程1

struts2启程之基本配置、核心概念及原理

1、struts2执行需要的几个文件:

web.xml、struts.xml、jsp页面以及Action类

a.web.xml中添加struts2过滤器,

需要注意的是:

A、struts2现在是作为过滤器使用,需要的过滤器类是org.apache.struts2.dispatcher.FilterDispatcher

B、如果将url-pattern设置成 /* 后,表示所有的url都会使用这个过滤器

xml 代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   <filter>  
  8.     <filter-name>wjl_strutsfilter-name>  
  9.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>  
  10.   filter>  
  11.   <filter-mapping>  
  12.     <filter-name>wjl_strutsfilter-name>  
  13.     <url-pattern>/*url-pattern>  
  14.   filter-mapping>  
  15. web-app>  

b.struts.xml文件必须位于classes根目录下

需要注意的是:

A、struts.xml文件中的头部dtd声明部分可以直接拷贝下载下来的struts2提供的apps中所用的struts.xml文件的头部dtd声明部分

B、action的name属性值为jsp页面中form表单的action值,如这里jsp的form应该是action="login.action"

C、result的name属性值为Action类execute方法返回的字符串值,result之间的值是指执行晚execute后dispatche到哪个jsp页面

 

xml 代码
  1. xml version="1.0" encoding="UTF-8" ?>  
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.        
  6.     <package name="wjl_struts" extends="struts-default">  
  7.         <action name="login" class="com.test.action.LoginAction">  
  8.             <result name="success">/result.jspresult>  
  9.         action>  
  10.     package>  
  11.        
  12. struts>  

 

2、struts执行流程

通过jsp页面中的form的action,将表单提交到通过struts.xml文件配置的Action类中,在这个类中,通过set属性名的方式使得对应的属性获得从Jsp页面中传递过来的对应的表单元素的值,并通过执行该类的execute方法返回一个字符串,并在struts.xml文件对应的result列表中去寻找与此字符串对应的那个result值,如果找到了,那么将dispatche到该值所对应的jsp页面中,该页面通过jstl标签${ requestScope.username }的方式来获得Action类中的属性值,并显示。

3、示例代码:

web.xml 代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   <filter>  
  8.     <filter-name>wjl_strutsfilter-name>  
  9.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>  
  10.   filter>  
  11.   <filter-mapping>  
  12.     <filter-name>wjl_strutsfilter-name>  
  13.     <url-pattern>/*url-pattern>  
  14.   filter-mapping>  
  15. web-app>  

 

struts.xml 代码
  1. xml version="1.0" encoding="UTF-8" ?>  
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.        
  6.     <package name="wjl_struts" extends="struts-default">  
  7.         <action name="login" class="com.test.action.LoginAction">  
  8.             <result name="success">/result.jspresult>  
  9.         action>  
  10.     package>  
  11.        
  12. struts>  

 

login.jsp代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>  
  6.   
  7. >  
  8. <html>  
  9.   <head>  
  10.     <base href="">  
  11.        
  12.     <title>My JSP 'login.jsp' starting pagetitle>  
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.       
  20.   
  21.   head>  
  22.      
  23.   <body>  
  24.        
  25.     <form action="login.action" method="post">  
  26.        
  27.     username:<input type="text" name="username"><br/>  
  28.     password:<input type="password" name="password"><br/>  
  29.        
  30.     <input type="submit" value="submit">  
  31.        
  32.     form>  
  33.        
  34.   body>  
  35. html>  

 

result.jsp 代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>  
  6.   
  7. >  
  8. <html>  
  9.   <head>  
  10.     <base href="">  
  11.        
  12.     <title>My JSP 'result.jsp' starting pagetitle>  
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.       
  20.   
  21.   head>  
  22.      
  23.   <body>  
  24.      
  25.     username:${ requestScope.username }<br/>  
  26.     password:${ requestScope.password }   
  27.      
  28.   body>  
  29. html>  

 

com.test.action.LoginAction 代码
  1. package com.test.action;   
  2.   
  3. public class LoginAction {   
  4.   
  5.     private String username;   
  6.     private String password;   
  7.     public String getUsername() {   
  8.         return username;   
  9.     }   
  10.     public void setUsername(String username) {   
  11.         this.username = username;   
  12.     }   
  13.     public String getPassword() {   
  14.         return password;   
  15.     }   
  16.     public void setPassword(String password) {   
  17.         this.password = password;   
  18.     }   
  19.        
  20.     public String execute() throws Exception   
  21.     {   
  22.         return "success";   
  23.     }   
  24. }   
评论
发表评论

您还没有登录,请登录后发表评论

yahaitt
搜索本博客
我的相册
24165a36-4704-3d78-a4aa-aa66ca678e03-thumb
ExtJs中关于grid和store的应用分析(一)
共 14 张
最近加入圈子
存档
最新评论