手动配置struts的全过程

java的struts框架的搭建,可以用工具自动的配置,虽然很方便.可是对于初学者而言,它隐藏了很多细节,不知道struts是如何遵循MVC模式的.
下面,我来给大家演示一下如何手动配置struts,希望能给大家一些帮助.
具体步骤:
1.先创建一个web工程
2.把struts开发包引入web工程(WEB-INF/lib)
3.编写login.jsp,wel.jsp和err.jsp
4.编写ActionForm(用户表单)和Action
5.编写struts-config.xml文件,该文件用于配置Action,ActionForm,对应关系,跳转位置,一般将这个文件放在/WEB-INF目录下.
6.配置web.xml文件,该文件用于配置ActionServlet
7.测试.
流程图如下:
下面我在说说具体的实现细节
1.web应用的目录如下
这里写图片描述
2将struts的全部jar包导入到WEB-INF/lib目录下
jar包在解压后的struts文件夹的lib下
这里写图片描述

3.login.jsp,err.jsp和wel.jsp是在WEB-INF目录下的,这样桌的目的是,使文件更安全.
login.jsp的代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/strutsLogin/login.do" method="post">
    u:<input type="text" name="usename"><br>
    p:<input type="password" name="passwd"><br>
    <input type="submit" value="login">
    </form>
</body>
</html>

err.jsp的代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
error
</body>
</html>

wel.jsp的代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
welcome!
</body>
</html>

因为login.jsp是放在WEB-INF目录下的,如何才能访问到呢?
在webroot下建立一个index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:include page="/WEB-INF/login.jsp"></jsp:include>
</body>
</html>

这样访问index.jsp,就会跳转到login.jsp文件去了
.
4.编写ActionForm(用户表单)和Action
ActionForm表单是用来存储数据的
UserForm.java的代码如下

package com.hsp.forms;
//这是一个用户表单,用于填充数据
import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {
    //定义属性[这里有一个规范,我们定义属性名字的时候]
    //应该和jsp页面的控件名称一样.如果有人提出疑问
    //说表单的属性名字一定和控件名字一样,不一定,
    //只要set方法和get方法的名字和控件一致.
    private String usename;
    private String passwd;
    public String getUsename() {
        return usename;
    }
    public void setUsename(String usename) {
        this.usename = usename;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

}

LoginAction.java如下

package com.hsp.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//这是一个action(表示小队长,需要继承action)
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.hsp.forms.UserForm;

public class LoginAction extends Action {

    //我们需要重写一个方法:execute会被自动调用
    //有点类似servlet/service/doGet/doPost方法

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //把form转成UserForm对象
        UserForm userForm=(UserForm)form;
        System.out.println("用户名="+userForm.getUsename());

        //简单验证
        if("123".equals(userForm.getPasswd())){
            //如果密码是123,就认为是合法用户
            //跳转到wel.jsp
            return mapping.findForward("ok");
        }else {
            return mapping.findForward("err");
        }
    }


}

5.编写struts-config.xml文件,该文件用于配置Action,ActionForm,对应关系,跳转位置,一般将这个文件放在/WEB-INF目录下.
struts-config.xml

<?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表单 -->
<form-beans>
<!-- name是表单名字,可以随意写 建议取名规范,表单类名小写-->
<!-- type用于指定表单类的全路径 -->
<form-bean name="userForm" type="com.hsp.forms.UserForm"/>
</form-beans>
<!-- 配置action -->
<action-mappings>
<!-- 配置具体的一个action -->
<!-- path:表示将来访问该action的资源名:
http://localhost:8080/web/path? -->
<!-- name:用于关联某个表单 -->
<!-- type:用于指定该action的全路径 -->
<action path="/login" name="userForm" type="com.hsp.actions.LoginAction">
<!-- 这里配置跳转关系 -->
<!-- name:表示结果名称 -->
<!-- path:表示转发到那个页面去 -->
<forward name="ok" path="/WEB-INF/wel.jsp"/>
<forward name="err" path="/WEB-INF/err.jsp"/>
</action>
</action-mappings>
</struts-config>

6.配置web.xml文件,该文件用于配置ActionServlet

<?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">
  <display-name>strutsLogin</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <!-- 配置struts-config --> 
  <init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

这样就配置完成了.访问index.jsp.
当第二个输入123时,就会跳转到wel.jsp页面
如果不是就会跳转到err.jsp中去.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值