Sturts的简单搭建

Struts的简单搭建


struts2

搭建

1 web.xml

[核心过滤器]

/* 过滤所有

StrutsPrepare

①纯手打
source





  Struts01

  

    index.html

    index.htm

    index.jsp

    default.html

    default.htm

    default.jsp

  

  

    

        struts

        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

    

    

        struts

        /*

    


②Design Filters

image.png

2.Action

package com.xxh.entity;

public class LoginAction {

private Integer password;

private String userName;

public String execute(){

      System.out.println("userName"+userName+"password"+password);

      return "success";

}

public Integer getPassword() {

      return password;

}

public void setPassword(Integer password) {

      this.password = password;

}

public String getUserName() {

      return userName;

}

public void setUserName(String userName) {

      this.userName = userName;

}

}

3.dtd 约束文件

struts.xml















/success.jsp







容易出错的地方

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login] associated with context path

struts.xml 在必须在src下,或者在namespace下修改路径,默认的struts.xml在src下

4.在WebRoot下的WEB-INF中的index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>





  

    

    My JSP 'index.jsp' starting page

        

  

   
用户名:
密 码:

创建一个登陆成功的界面

<%@ page language="java" import="java.util.*"

      pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



  

  

  

     欢迎您:${username}

  


注意事项
1web.xml中过滤器的名称可以随便起
2LoginAction notfound
struts.xml中的全类名写错了
3LoginAction 中的execute(),他是struts的默认的执行方法
4.配置文件名称必须是struts.xml,全小写
报404错:There is no Action mapped
5如果修改tomact默认的shutdown端口,服务器不能正常启动
6.web.xml核心过滤器必须有

执行流程
index.jsp--->请求[先找核心过滤器(web.xml)]---->映射到struts.xml文件---->实体类[login,返回值success]---->execute方法返回success---->success.jsp页面[el输出]
前后台传参;
前台属性的值
后台:封装成对象中的属性进行接收

//代码流程:
1:导包
2:配置web.xml
3:新建实体类
4:新建struts.xml
5:index
6:输出 success

你可能感兴趣的:(Sturts的简单搭建)