您的位置:首页 > 编程语言 > Java开发

struts2的HelloWorld

2015-08-12 09:20 531 查看
初学struts2,想写一个简单的helloworld程序,结果遇到好多问题。

IDE:IntelliJ 14网上大部分的都是eclipse下面的,在IntelliJ下面还是小有不同的。

程序是参照李刚的《struts2X权威指南》写的

首先建立好web项目,这个可以百度一下。有详细的教程http://jingyan.baidu.com/article/54b6b9c0f79b0c2d583b47d6.html

1,在web文件下新建一个Login.asp,代码如下:

<%--
Created by IntelliJ IDEA.
User: sgzhang
Date: 2015/8/11
Time: 14:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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 colspan="2"><input type="submit" value="登录"/>
<input type="reset" value="重填"/>
</td>
</tr>
</table>
</form>
</body>
</html>


2,建立一个供正确跳转的welcome.asp,代码如下:

<%--
Created by IntelliJ IDEA.
User: sgzhang
Date: 2015/8/11
Time: 15:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功页面</title>
</head>
<body>
欢迎,${sessionScope.user},你已经登录!
</body>
</html>


3,建立一个错误登录的页面 error.asp

<%--
Created by IntelliJ IDEA.
User: sgzhang
Date: 2015/8/11
Time: 15:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
登录失败!
</body>
</html>


4,后台控制代码:LoginAction.java 注意此文件务必放在src文件的一个package下面,否则报错。我的是在src下面新建了一个package名为Lee。这个Action 是通过实现Action 接口得到的(其实可以不需要)

package Lee;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
* Created by sgzhang on 2015/8/11.
*/

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 void setPassword(String password){
this.password=password;
}

public String getPassword(){
return password;
}

public String execute()throws Exception{
if(getUsername().equals("scott")
&&getPassword().equals("tiger")){
ActionContext.getContext().getSession().put("user",getUsername());//session
return SUCCESS;
}else{
return ERROR;
}
}
}


5,接下来是配置struts.xml文件,此文件在src下找到。配置代码如下

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="strutsqs"  extends="struts-default">
<action name="Login" class="Lee.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>


6。运行程序,输入localhost:8080/Login.jsp,就可以看到如下页面



输入用户名scott,密码tiger就可以看到登录成功的提示了



可以看到这个程序用了session保存了用户名
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: