struts标签获得不了jsp页面的值(username,password打印出来都是null) !!!!!急求啊!!!
LoginAction类:packagecom.actions;importcom.opensymphony.xwork2.ActionSupport;publiccla...
LoginAction类:
package com.actions;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
String username;
String password;
public String getUsername(){
return username;
}
public void setUsername(){
this.username = username;
}
public String getPassword(){
System.out.print(password);
return password;
}
public void setPassword(){
this.password = password;
}
public String execute() throws Exception{
System.out.print("xax" + getUsername() + " "+getPassword());
return SUCCESS;
}
}
JSP:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Login</title>
//中间一堆meta啥的去掉的,空间不够
</head>
<body>
<div align="right">
<s:a href = "login_success.jsp">
<font color ="blue"><h5>
还未注册?点击注册
</h5>
</font>
</s:a>
</div>
<div style="color:#7FFF00">
<s:form action="login.action" method="post">
<s:textfield name="username" label="账号"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value ="登录"></s:submit>
</s:form>
</div>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.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>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name ="login" class="com.actions.LoginAction">
<result name ="success">/login_success.jsp</result>
</action>
</package>
</struts> 展开
package com.actions;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
String username;
String password;
public String getUsername(){
return username;
}
public void setUsername(){
this.username = username;
}
public String getPassword(){
System.out.print(password);
return password;
}
public void setPassword(){
this.password = password;
}
public String execute() throws Exception{
System.out.print("xax" + getUsername() + " "+getPassword());
return SUCCESS;
}
}
JSP:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Login</title>
//中间一堆meta啥的去掉的,空间不够
</head>
<body>
<div align="right">
<s:a href = "login_success.jsp">
<font color ="blue"><h5>
还未注册?点击注册
</h5>
</font>
</s:a>
</div>
<div style="color:#7FFF00">
<s:form action="login.action" method="post">
<s:textfield name="username" label="账号"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value ="登录"></s:submit>
</s:form>
</div>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.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>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name ="login" class="com.actions.LoginAction">
<result name ="success">/login_success.jsp</result>
</action>
</package>
</struts> 展开
展开全部
前面一个人说的对你的setter方法没有参数,估计因为这个jsp的值赋不到action对应的变量中。
setter和getter可以右键编译地方然后选Source--Generate Getters and Setters,然后选择你要生成的变量,最后选择ok,这样不大会容易出错。
setter和getter可以右键编译地方然后选Source--Generate Getters and Setters,然后选择你要生成的变量,最后选择ok,这样不大会容易出错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我试过你的代码,发现错误在于你的LoginAction代码,这两个set方法没写对,应该有参数的,否则没法赋值的。
public void setUsername(String username){
this.username = username;
}
public void setPassword(String password){
this.password = password;
public void setUsername(String username){
this.username = username;
}
public void setPassword(String password){
this.password = password;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把你那些taglib去掉,都改成HTML代码试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<package name="default" namespace="/" extends="struts-default">
你的package name=default?
其他看不出什么问题 难道是这个命名有问题?
你的package name=default?
其他看不出什么问题 难道是这个命名有问题?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有发现错误 你的代码应该是没为题的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询