一个简单关于struts2的login问题。
JAVA代码:
package cn.cjh.action;import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;public class LoginAction implements Action {
private String username;
private String password;
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
Integer counter = (Integer)ctx.getApplication().get("counter");
if(counter == null)
{
counter = 1;
}
else
{
counter += 1;
}
ctx.getApplication().put("counter", counter);
ctx.getSession().put("user", getUsername());
if(getUsername().equals("cjh")&&getPassword().equals("cjh"))
{
ctx.put("tip", "登录成功");
return SUCCESS;
}
else
{
ctx.put("tip", "不成功");
return ERROR;
}
}}
struts.xml文件位置放在SRC下。
<?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>
<package name="struts2" extends="struts-default">
<action name="login" class="cn.cjh.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
login.jsp 代码:
<%@ page contentType="text/html; charset=GBK" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center">
<caption><h3>用户登录</h3></caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr>
<td>密 码:<input type="text" name="password"/></td>
</tr>
<tr align="center">
<td><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
</tr>
</table>
</form>
</body>
</html>
报的错误:
HTTP Status 404 - /cjh/struts2/WebRoot/login.action--------------------------------------------------------------------------------type Status reportmessage /cjh/struts2/WebRoot/login.actiondescription The requested resource (/cjh/struts2/WebRoot/login.action) is not available.
代码和书上的一样。但是一直报这个错误。网上找了许多答案 依旧报错。求各位大虾帮助啊!!!!
JAVA代码:
package cn.cjh.action;import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;public class LoginAction implements Action {
private String username;
private String password;
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
Integer counter = (Integer)ctx.getApplication().get("counter");
if(counter == null)
{
counter = 1;
}
else
{
counter += 1;
}
ctx.getApplication().put("counter", counter);
ctx.getSession().put("user", getUsername());
if(getUsername().equals("cjh")&&getPassword().equals("cjh"))
{
ctx.put("tip", "登录成功");
return SUCCESS;
}
else
{
ctx.put("tip", "不成功");
return ERROR;
}
}}
struts.xml文件位置放在SRC下。
<?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>
<package name="struts2" extends="struts-default">
<action name="login" class="cn.cjh.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
login.jsp 代码:
<%@ page contentType="text/html; charset=GBK" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center">
<caption><h3>用户登录</h3></caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr>
<td>密 码:<input type="text" name="password"/></td>
</tr>
<tr align="center">
<td><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
</tr>
</table>
</form>
</body>
</html>
报的错误:
HTTP Status 404 - /cjh/struts2/WebRoot/login.action--------------------------------------------------------------------------------type Status reportmessage /cjh/struts2/WebRoot/login.actiondescription The requested resource (/cjh/struts2/WebRoot/login.action) is not available.
代码和书上的一样。但是一直报这个错误。网上找了许多答案 依旧报错。求各位大虾帮助啊!!!!
解决方案 »
- 运算符的 运用
- java解析dbf格式时里面数据类型怎么判断,急急。。
- SSH下所有DispatchAction问题····
- Java 正则表达式问题
- 为什么客户调用不了Xfire Service服务端呢,,帮帮我~~~~~~
- 多数据库操作的数据连接
- hibernate执行insert操作出错,急!
- 100分求在java中用代码实现将一个xml文件以Html格式显示出来
- 关于jdbc的问题
- 已经装了jdk 1.4,现在装javabean development kit (BDK1.1)安装过程中有一步是choose virtual machine ,java虚拟机
- Cannot create a session after the response has been committed
- java如何获取服务器的当前时间----------------
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<form action="/login.action" method="post">在struts.xml试着加
<package name="struts2" extends="struts-default" namespace="/">再试试看、
找不到action
启动tomcat: http://localhost:8080/cjh/login.action
或者 修改 tomcat\conf\server.xml在 </Host>前加入
<Context debug="0" docBase="盘符/cjh/struts2/WebRoot" path="/a" reloadable="true" >
</Context>
</Host>启动tomcat: http://localhost:8080/a/login.action