目录
因为公司有个别项目是用SSH框架做的,所以就准备学习以下struts2框架,在网上搜了一下struts2的教程(w3的),刚看到创建demo的时候,就出现了一大堆问题,后来一看教程就错了,折磨了我一下午,以下把问题和demo罗列出来:
Demo
HelloWorldAction
package com.struts.controller;
public class HelloWorldAction {
private String name ;
private String age;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
struts2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello"
class="com.struts.controller.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Struts 2</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.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
name, <s:property value="getName()"/><br>
age, <s:property value="age"/>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action="hello">
<input type="text" name="name"/>
<input type="text" name="age"/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>
问题
1,jar包资源的问题(注意jar包版本,我这里用的是2.2.3)
struts2的jar包依赖兼容性真是太差了,多了也不行,少了也不行
2,过滤器的问题
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
目前是发现了三个版本(具体版本号记不住了,仅供参考)
2.2以下的是
org.apache.struts2.dispatcher.FilterDispatcher
2.2到2.3的是
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
2.3以上的是
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
3,程序报错(There is no Action mapped for namespace / and action name)
大概意思就是找不到对应的Action,网上查到的说法有很多,大概就是说struts2.xml没加载到,idea我是这么解决的
4,<s:property value="age"> 取不到值的问题