关于struts2的There is no Action mapped for namespace / and action name login.
一个简单的登录验证,不多说,直接贴代码:
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
</head>
<body>
<div>
<center>
<s:form action="/login.action" method="post">
<!-- 账户输入框 -->
<s:textfield name="userno" label="账号"></s:textfield>
<!-- 密码输入框 -->
<s:password name="password" label="密码"></s:password>
<!-- 确定与重置按钮 -->
<s:submit value="确定"></s:submit>
<s:reset value="重置"></s:reset>
</s:form>
<!-- 注册新用户链接 -->
<a href="regist.jsp">注册新用户</a>
</center>
</div>
</body>
</html>
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 设置srtuts2框架的 -->
<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>
<?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>
<!-- <constant name="struts.i18n.encoding" value="GBK"></constant>
<constant name="struts.custom.i18n.resources" value="globalMessages"></constant>-->
<package name="login" extends="struts-default">
<action name="LoginAction" class="bank.action.LoginAction" method="login">
<result name="loginSuccess">/trade.jsp</result>
<result name="loginError">/loginError.jsp</result>
</action>
<action name="LoginAction" class="bank.action.LoginAction" method="logout">
<result name="logout">/logout.jsp</result>
</action>
</package>
</struts>
package bank.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import bank.facade.UserService;
import bank.facade.impl.UserServiceImpl;
import bank.model.UserInfo;
public class LoginAction extends ActionSupport {
private String userno;
private String password;
private UserService userservice;
public LoginAction(){
userservice = new UserServiceImpl();
}
public String getUserno() {
return userno;
}
public void setUserno(String userno) {
this.userno = userno;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//实现登录功能
public String login(){
boolean flag = false;
UserInfo user = new UserInfo();
user.setUserno(getUserno());
user.setPassword(getPassword());
flag = userservice.login(user);
if(flag==true){
UserInfo userinfo = userservice.selectUser(userno);
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
session.setAttribute("user", userinfo);
return "loginSuccess";
}else{
return "loginError";
}
}
//实现退出功能
public String logout(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
session.invalidate();
return "logout";
}
}