大学时学过,现在好多年忘记的差不多了。那时候学的是SSH架构,不过用的struts1,现在工作之余想重温下。
用的Struts2。
做了个简单的登录,如果账号密码正确,转到welcome.jsp,不正确转error.jsp。可是不管我输什么,都是跳转到welcome.jsp。以下是代码。
LoginAction.java
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void init(){
System.out.print("Hello");
}
public String Execute() throws Exception{
if("admin".equals(name) &&"admin".equals(password)){
System.out.print("成功");
return "success";
}
else {
System.out.print("失败");
return "error";
}
}
}
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- action的属性值对应struts.xml中action的name值 -->
<form action="login" method="post">
<p>用户登录:</p>
<p>
用户名:<input type="text" name="username">
</p>
<p>
密 码:<input type="password" name="password">
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login" class="com.action.LoginAction" method="execute">
<result name="success">welcome.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
各位大神,帮忙看看是哪里问题。