Struts的简单搭建(1)

Struts的简单搭建


struts2

搭建

1 web.xml

[核心过滤器]

/* 过滤所有

<filter-class> StrutsPrepare

①纯手打
source

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" xmlns="[http://xmlns.jcp.org/xml/ns/javaee](http://xmlns.jcp.org/xml/ns/javaee)" xsi:schemaLocation="[http://xmlns.jcp.org/xml/ns/javaee](http://xmlns.jcp.org/xml/ns/javaee)[http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd](http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd)" id="WebApp_ID" version="3.1">


  <display-name>Struts01</display-name>


  <welcome-file-list>


    <welcome-file>index.html</welcome-file>


    <welcome-file>index.htm</welcome-file>


    <welcome-file>index.jsp</welcome-file>


    <welcome-file>default.html</welcome-file>


    <welcome-file>default.htm</welcome-file>


    <welcome-file>default.jsp</welcome-file>


  </welcome-file-list>


  <!--  -->


    <filter>


        <filter-name>struts</filter-name>


        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>


    </filter>


    <filter-mapping>


        <filter-name>struts</filter-name>


        <url-pattern>/*</url-pattern>


    </filter-mapping>


</web-app>

②Design Filters

这里写图片描述

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

<?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](http://struts.apache.org/dtds/struts-2.3.dtd)">


<struts>


<!--注册包 包的名字可以随便取-->


<package name="xxx" namespace="/" extends="struts-default">


<!-- 注册类  name是前台需要传参的地址 class是Action的全类目,方便能找到Action-->


<action name="login" class="com.xxh.entity.LoginAction">


<result name="success">/success.jsp</result>


</action>


</package>


</struts>

容易出错的地方

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+"/";


%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>


  <head>


    <base href="<%=basePath%>">


    <title>My JSP 'index.jsp' starting page</title>


        </head>


  <body>


   <form action="login" method="post">


      用户名:<input type="text" name="username"><br>


      密&nbsp;码:<input type="password" name="password"><br>


      <input type="submit" value="登录">


    </form>


  </body>


</html>

创建一个登陆成功的界面

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


      pageEncoding="UTF-8"%>


<%


String path = request.getContextPath();


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


%>


<html>


  <head>


  </head>


  <body>


     欢迎您:${username}


  </body>


</html>

注意事项
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

猜你喜欢

转载自blog.csdn.net/qq_31334119/article/details/80082058