打赏

Struts2_learning

一.这是我学习struts2所做的一个记录,因为整个过程较为麻烦,所以,记录下来,以便以后使用

过程:

 

步骤:

1)dynamic web project

2)jars

3)struts.xml

package,action,result,jsp pages

web.xml(filter)

开始:

1.新建一个Dynamic web project

2.注意,选择apach 2.5的就好,不然,后面你会再选要选择一个xml文件,比较麻烦,所以,我们这里直接选择2.5

3.接下来,我们就要去配置一下,我们所需要的那些jar包了,总共有10个,我放在百度云里面,有需要的自取。

链接:https://pan.baidu.com/s/1SVaiolxyhRrNg-KSqd8wSA 密码:asey

4.新建一个struts.xml文件,直接在src那里右键即可

5.注意编程时要联网,不然会有各种各样的问题

6.接下来,struts.xml注意加上一句

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">

这句用来在访问时,访问云端

7.新建一个index.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>Insert title here</title>
</head>
<body>
<form action="Login.action" method="post">
User:<input type="text" name="user"/><br/>
Pass:<input type="password" name="pass"/><br/>
<input type="submit" value="Login"/>

</body>
</html>

8.接下来配置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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>teststruct2</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>
   <!-- 配置Struts2过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping> 
  
</web-app>

9.此时在struts.xml配置好后

10.新建一个action 注意在新建的时候,命名为LoginAction

直接输入ActionSupp第一个就是

12.程序都编好后,一定要注意,是否有将那jar包包含在web.inf中lib文件夹中,我就是没包含,导致出现classnotfound的错误。

13.并且,struts的各项一定要注意大小写,及姓名。

14.给一个我当时出现错误的解决办法链接:https://blog.csdn.net/hbbonson/article/details/8285209

现将我的整个程序打包于百度云中,需要的自取。

链接:https://pan.baidu.com/s/1E99rp4xHpm-SC0doGn1NKA 密码:a51l

15.个人觉得这个程序意义不太,因为确实配置那些jar包需要花费的时间异常的大。后续还会出maven,整个配置会容易的多。

注意事项:

这里再提及几个需要注意的事项:

1.

这里的struts.xml,名字一定要对,且一定要放在src下面,否则容易出错。

2.

这里的jar一定要配置好

3.

一定要把这些jar包弄进lib

4.

这里的action=“Login.action ”后缀一定要用.action,因为这样struts才会将其当为需要传送的action,在name 那个位置进行判断。

并跳转到相应的

这个地方。

5.

此为url的过滤器。

6. mvc据我的理解是:

首先用户在index发起请求,->跳转到struts.xml进行name的判断--->再跳转到相应的处理类 Loginaction那个地方--->处理判断完后,

返回一个值->struts.xml中的result根据name进行判断->再进行具体的跳转。

7.这里再贴出一个struts2mvc的相关链接:https://blog.csdn.net/li_qinging/article/details/72809789

2018-05-17

今天对里面程序的具体再进行一次比较具体的讲解。

struts.xml的配置

    <package name="abc" namespace="/" extends="struts-default"><!--  package="?"?为模块的名字,并且需要继承struts-default -->

并且,值得注意的是,后面再访问的时候http://3036/input.jsp

假如

这样的话,有可能会出现错误,应在跳转页面输入namespace的值

例如

http://3036/hello/input.jsp

所以,一般命名为“/”是最为恰当的,不会出现这个问题

 

6.

这是result的一个内在机制

7.多个字节点使用name来区分,默认值问“success”

8.= = =

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2018-05-21 20:53  idoe  阅读(137)  评论(0编辑  收藏  举报