eclipse+maven 使用struts 2
1.创建项目 web-app
2.创建成功后的目录以及解决报错
解决报错 加入apache的Tomcat包且全部选入资源包
目录如下:
加入pom依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.2</version>
</dependency>
web配置
< web-app>
< display-name>Archetype Created Web Application</ display-name>
< welcome-file-list>
< welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>/*</url-pattern>
< /filter-mapping>
</web-app>
在次配置web.xml可能会有报错 需要手动调整规则 例如删掉最上面的注释等
加入action与视图
index.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8"%>
< html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>Hello World!</h2>
<form action="loginAction">
姓名<input type='text' name="Name"><input type="submit" value="提交">
</form>
</body>
</html>
result.jsp
< body>
Hello <s:property value=“Name” />
< /body>
struts.xml
< struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.locale" value="zh_CN"></constant>
<package name="hurricane" extends="struts-default">
<action name="loginAction" class="com.demo.LoginAction" method="execute">
<result>
/result.jsp
</result>
</action>
</package>
</struts>
最后结构
运行结果: