写了Action的应用程序,点击index.jsp的连接,就报错404了,求解决呀!
我在自学,望大家多指教!
下面是程序所有代码:
index.jsp代码
<body>
<a href="userAction!add">添加用户</a><br>
<a href="userAction!update">更新用户</a>
</body>
struts.xml代码
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="userAction" class="com.struts2.UserAction">
<result name="add">/user_add.jsp</result>
<result name="update">/user_update.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
web.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
UserAction.java代码
package com.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID=1L; //主要用于版本控制
private String info;
public String add() throws Exception{
info="添加用户信息";
return "add";
}
public String update() throws Exception{
info="更新用户信息";
return "update";
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
user_add.jsp代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>user_add</title>
</head>
<body>
<font color="red">
<s:property value="info" />
</font>
</body>
</html>
user_update.jsp代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset="
pageEncoding=""%>
<meta http-equiv="Content-Type" content="text/html; charset=">
<title>user_updae</title>
</head>
<body>
<font>
<s:property value="info" />
</font>
</body>
</html>
<action name="userAction_*" class="com.struts2.UserAction" method="{1}"> <result name="add">/user_add.jsp</result> <result name="update">/user_update.jsp</result> </action>
<package name="default" namespace="/" extends="struts-default">
<action name="userAction" class="com.struts2.UserAction" method=""> ---默认方法是String execute()
<result name="add">/user_add.jsp</result>
<result name="update">/user_update.jsp</result> ----是execute返回结果
</action>