IDEA与Eclipse创建struts项目

1、IDEA创建struts项目

这里再构建struts项目是选择jar包出问题了,可以重新配置

 

 

 

创建页面和action配置struts.xml

启动tomcat,浏览器中运行

具体参考:

https://www.jianshu.com/p/ef5b9ed1cdb8

2 eclipse中创建

第一步:创建Web工程

   创建一个Web工程,名为day01_struts2_demo01
第二步:导jar包
   把struts2-blank.war中的lib下所有的包导入自己创建的工程
第三步:添加配置文件
  把struts2-blank里classes目录下的struts.xml配置文件拷贝到自己创建工程的src中
  然后删除struts标签的所有内容,我们一步步讲struts标签内容的知识
第四步:配置struts的过滤器
  在web.xml中,配置Filter

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

<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

 配置的目的是用于拦截请求,由Struts的规则去处理请求,而不是用以前的servlet去处理
第五步:Tomcat运行Web工程
   如果tomcat运行没有报错,就代表配置成功
Struts 的Action配置讲解
目标:配置一个请求的完整流程
第一步:
   在struts中配置package和action,写如下代码
 
第二步:
  写一个HelloAction的类,需要写个sayHello方法
 
第三步:
   在WebContent中添加一个success.jsp页面
 
第四步:
  浏览器访问下面三个路径
  http://localhost:8080/day01_struts2_demo01/hello 【能正常访问】
  http://localhost:8080/day01_struts2_demo01/hello.action 【也能正常访问】
  http://localhost:8080/day01_struts2_demo01/hello.act 【不能正确访问】
   因为struts默认是处理.action的请求,或者不带action也可以,其它后缀的都不会拦截,会放行,如jsp
 

猜你喜欢

转载自www.cnblogs.com/GZBSYS/p/10404442.html
今日推荐