是按最簡(jiǎn)單的例子在寫的,但總是404.。
本人小白,剛?cè)雜truts2.之前用的2.5.17版,但是沒(méi)有blank。war。。所以回裝了2.3版本。但是實(shí)驗(yàn)最簡(jiǎn)單的action例子總是失敗。代碼如下:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
?<display-name>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置核心攔截器 -->
<filter>
??? <!-- Filter的名字 -->
??? <filter-name>struts2</filter-name>
??? <!-- Filter的實(shí)現(xiàn)類 struts2.5以前可能有所不同 -->
??? <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>???
<filter-mapping>
??? <filter-name>struts2</filter-name>
??? <!-- 攔截所有的url -->
??? <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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" strict-method-invocation="false">
??????? <!-- name action的名字,訪問(wèn)時(shí)使用helloworld.action訪問(wèn),class:實(shí)現(xiàn)類 -->
??????? <action name="helloworld" class="cn.xhcoding.action.HelloWorldAction">
??????????? <!-- 結(jié)果集,即action中SUCCESS返回的視圖 -->
??????????? <result>/result.jsp</result>
??????? </action>
??????? </package>
??? </struts>
action類
package cn.xhcoding.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
?@Override
??? public String execute() throws Exception {
??????? System.out.println("正在執(zhí)行的Action");
??????? // 返回視圖 SUCCESS,這是框架定義
??????? return SUCCESS;
??? }
}
result.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>Action Result</title>
</head>
<body>
<h1>恭喜成功配置好基本的struts2環(huán)境</h1>
<h2>Hello World, I am Successful</h2>
</body>
</html>
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>Action Result</title>
</head>
<body>
<h1>index</h1>
</body>
</html>
.可以顯示
2019-01-06
訪問(wèn)的網(wǎng)址有問(wèn)題吧?