Struts 2 初步入门(一)

xiaoxiao2021-02-28  45

1. 搭建Struts 2环境步骤

下载jar----->创建web项目---->创建并完善相关配置文件---->创建action并测试启动

下载jar包访问网站:http//struts.apache.org

(1)在下载的Struts2.3.34/lib包下选择8jar包到创建的web项目的lib文件夹

(2)完善web.xml文件如下:

<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>

(3) web项目下,建一个java文件

package com.imooc.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport{ @Override public String execute() throws Exception { System.out.println("执行excute()"); return SUCCESS; } }

(4) 配置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"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class ="com.imooc.action.HelloWorldAction"> <result>/result.jsp</result> </action> </package> </struts>

(5) 创建result.jsp 文件

(6) 启动Tomcat服务器,在浏览器导航栏输入:http://localhost:8080/0408/hello.action,其中0408为工程名称。

期间遇到问题如下:

1.导入包的问题,少了包,或者导错了包,都会报错。

2.导入包后,IDE配置问题,如下:

今天在配置Struts2开发环境的时候,遇到了一个错误,如下:

原因为:使用了IDE默认的配置,少配置了struct-default.xml文件,修改方法为先删除默认配置,再手动添加配置文件:

进入Project Settings下的Modules中,我们首先要删除调默认的File Sets,如果你之前配置过,也要全部删除,这里我们要重新配置, 

删除完后IEDA会给我们提示 。

此后,勾选struts.xml文件和Struts-default.xml文件。

转载请注明原文地址: https://www.6miu.com/read-2625576.html

最新回复(0)