Struts开发框架实例 及注意事项

来源:互联网 发布:网络借钱不还的后果 编辑:程序博客网 时间:2024/05/03 06:54

在刚开始写第一个框架案例时:

    一.注意各个类型的文件的存储位置:



   二.特别注意web.xml和struts.xml的文件配置不能出错,下面是我配置的两个代码片:

            web.xml:
               
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"      id="WebApp_ID" version="3.0">   <session-config>       <session-timeout>           30       </session-timeout>   </session-config>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <filter>      <filter-name>          struts2      </filter-name>      <filter-class>          org.apache.struts2.dispatcher.FilterDispatcher      </filter-class>  </filter>  <filter>      <filter-name>          struts2-cleanup      </filter-name>      <filter-class>          org.apache.struts2.dispatcher.ActionContextCleanUp      </filter-class>  </filter>  <filter-mapping>      <filter-name>          struts2      </filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>  <filter-mapping>      <filter-name>          struts2-cleanup      </filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping></web-app>
 
            struts.xml:
        
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constant name="struts.ui.theme" value="simple" /><package name="test" extends="struts-default"><action name="sayWelcome" class="myActions.sayHello"><result name="success">/welcome.jsp</result><result name="input">/index.jsp</result></action></package></struts>
 

另外的几个.jsp文件和.java文件如下
          
             index.jsp:
    
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>第一个Struts 2应用</title></head><body>    <s:form action="sayWelcome">               请输入用户名:<s:textfield name="username" size="20"/>     <s:submit value="确定"/>     <s:reset value="重写"/>    </s:form>    <s:if test="hasFieldErrors()">      <s:fielderror/>    </s:if></body></html>
         
               




                   welcome.jsp:
 
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登录成功</title></head><body>   ${username} ,欢迎光临!</body></html>

                 
               


                   sayHello.java:


package myActions;import com.opensymphony.xwork2.ActionSupport;public class sayHello extends ActionSupport {   private String username;   String getUsername(){return username;}   public void setUsername(String username){this.username=username;}   public String execute() throws Exception{   return SUCCESS;   }   public void validate(){   if(username==null||username.trim().length()<5){   addFieldError("username","用户名错误!");   }   }}

 




 
 
原创粉丝点击