jsp代碼,就是在body上加個表單,表單內容如下:
<form action="login.action" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="user.username"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr>
<td><input type="reset" value="重填"></td>
<td><input type="submit" value="登錄"></td>
</tr>
</table>
</form>
然後是Action
public class LoginAction extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
@Override
public String execute() throws Exception
{
if("zhangsan".equals(user.getUsername())&&"123".equals(user.getPassword()))
{
System.out.println("SUCCESS 結果");
return SUCCESS;
}
else
{
System.out.println("error結果");
return ERROR;
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
再然後是struts2的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" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
最後是一個javabean
public class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
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;
}
}
error.jsp與success.jsp隨便寫就好,然後運行就一直報No result defined for action com.action.LoginAction and result 的錯誤,只要把struts.xml文件跟上面給出的jsp form裡的login改成其他任意名字,例如:hhh之後,就正常運行不再報錯。這是為何?求各位幫個忙解答下,感激不盡
web.xml中struts配置的問題。祝好!
<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>