使用手动配置的方式开发第一个Struts项目的步骤以及实例

一 先创建一个web工程


 
 
二 引入struts的开发包到项目
 
三 编写login.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding = "ISO-8859-1" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'login.jsp' starting page </ title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
         <!--
        <link rel =" stylesheet " type="text/ css " href ="styles.css">
        -->
 
  </ head >
 
  < body >
    < form action = "/strutslogin/login.do" method = "post" >
        u: < input type = "text" name = "username" >< br />
        p: < input type = "password" name = "password" >< br />
         < input type = "submit" value = "login" >
    </ form >
  </ body >
</ html >
 
四 编写ActonForm(用户表单)和Action(登录小队长)
package com.cakin.forms;
//这是一个用户表单,用于填充数据的
import org.apache.struts.action.ActionForm;
 
public class UserForm extends ActionForm {
         //定义属性【属性名应该和 jsp 页面的控件名称一致】
         //如果有人提出疑问:表单名字是不是一定要和控件名称一样?不一定,但要保证get和set方法的名字和
         //控件名称一致
         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;
        }
        
}


 
package com.cakin.actions;
//这是一个action(表示小队长,需要继承Action)
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
import com.cakin.forms.UserForm;
 
public class LoginAction extends Action {
         //我们需要重新编写一个方法:execute会被自动调用,有点类似 servlet 的service方法。
         @Override
         public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                         throws Exception {
                         //把form转成对应的UserForm对象
                UserForm userForm=(UserForm)form;
 
                 //简单验证
                 if ( "123" .equals(userForm.getPassword())){
                         return mapping.findForward( "ok" );
                }
                 else {
                         return mapping.findForward( "err" );
                }
        }
}
 
五 编写struts-config.xml文件,该文件用于配置action actionForm以及对应关系,跳转位置,一般放在/WEB-INF目录下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" " http://struts.apache.org/dtds/struts-config_1_3.dtd"> ;
<struts-config>
  <!-- 配置表单 -->
  <form-beans>
      <!-- name是表单名字,可以随意写,但我们建议取名规范,表单为类名小写 -->
      <!-- type用于指定表单类全路径 -->
      <form-bean name="userForm" type="com.cakin.forms.UserForm"></form-bean>
  </form-beans>
  <action-mappings>
    <!-- 配置具体的一个action, path:表示将来访问action的url http://localhost:8080/web/path -->
      <!--  name用于关联某个表单-->
      <!--  type用于指定该action类全路径-->
      <action path="/login" name="userForm" type="com.cakin.actions.LoginAction">
      <!--  这里配置跳转关系-->
          <!--name表示结果名称 path:转发到哪个页面  -->
          <forward name="ok" path="/WEB-INF/wel.jsp"></forward>
          <forward name="err" path="/WEB-INF/err.jsp"></forward>
      </action>
      
  </action-mappings>
</struts-config>
 
六 写出welcome.jsp页面
<%@ page language = "java" import = "java.util.*" pageEncoding = "ISO-8859-1" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
  < head >
    < base href = " <%= basePath %> " >
   
    < title > My JSP 'wel.jsp' starting page </ title >
   
         < meta http-equiv = "pragma" content = "no-cache" >
         < meta http-equiv = "cache-control" content = "no-cache" >
         < meta http-equiv = "expires" content = "0" >    
         < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
         < meta http-equiv = "description" content = "This is my page" >
         <!--
        <link rel =" stylesheet " type="text/ css " href ="styles.css">
        -->
 
  </ head >
 
  < body >
    welcome < br >
  </ body >
</ html >
 
七 配置web.xml中的ActionServlet
<? xml version = "1.0" encoding = "UTF-8" ?>
  < display-name />
  < servlet >
    < servlet-name > action </ servlet-name >
    < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
    <!-- 配置struts-config.xml -->
    < init-param >
      < param-name > config </ param-name >
      < param-value > /WEB-INF/struts-config.xml </ param-value >
    </ init-param >
    < init-param >
      < param-name > debug </ param-name >
      < param-value > 3 </ param-value >
    </ init-param >
    < init-param >
      < param-name > detail </ param-name >
      < param-value > 3 </ param-value >
    </ init-param >
    < load-on-startup > 0 </ load-on-startup >
  </ servlet >
  < servlet-mapping >
    < servlet-name > action </ servlet-name >
    < url-pattern > *.do </ url-pattern >
  </ servlet-mapping >
  < welcome-file-list >
    < welcome-file > index.jsp </ welcome-file >
  </ welcome-file-list >
</ web-app >
 
八 测试


 

猜你喜欢

转载自cakin24.iteye.com/blog/2397492