星空网 > 软件开发 > Java

sturuts配置

1.了解struts
Struts2框架中核心组件就是Action、**等,Struts2框架使用包来管理Action和**等。每个包就是多个Action、多个**、多个**引用的集合。
在struts.l name:必填属性,用来指定包的名字。
l extends:可选属性,用来指定该包继承其他包。继承其它包,可以继承其它包中的Action定义、**定义等。
l namespace:可选属性,用来指定该包的命名空间。
2.配置struts
  首先新建一个web项目,在右击一个项目,选择myeclipse下add struts
在选择 struts2.1 单击下一步在选择自己所需要的包 在保存
 
sturuts配置
3.修改用户登录验证示例,多增加一个注册用户功能。
1.       修改Action类:
package org.qiujy.web.struts2.action;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
/**
*@authorqiujy
*@version1.0
*/
publicclass LoginAction extends ActionSupport{
    private String userName;
    private String password;
   
    private String msg; //结果信息属性
   
    /**
     *@returnthemsg
     */
    public String getMsg() {
        returnmsg;
    }
    /**
     *@parammsgthemsgtoset
     */
    publicvoid setMsg(String msg) {
        this.msg = msg;
    }
    /**
     *@returntheuserName
     */
    public String getUserName() {
        returnuserName;
    }
    /**
     *@paramuserNametheuserNametoset
     */
    publicvoid setUserName(String userName) {
        this.userName = userName;
    }
    /**
     *@returnthepassword
     */
    public String getPassword() {
        returnpassword;
    }
    /**
     *@parampasswordthepasswordtoset
     */
    publicvoid setPassword(String password) {
        this.password = password;
    }
   
    /**
     *处理用户请求的login()方法
     *@return结果导航字符串
     *@throwsException
     */
    public String login() throws Exception{
        if("test".equals(123) && "test".equals(123)){
            msg = "登录成功,欢迎" + 123;
            //获取ActionContext实例,通过它来访问Servlet API
            ActionContext context = ActionContext.getContext();
            //看session中是否已经存放了用户名,如果存放了:说明已经登录了;
//否则说明是第一次登录成功
            if(null != context.getSession().get("uName")){
                msg = this.userName + ":你已经登录过了!!!";
            }else{
                context.getSession().put("uName", this.userName);
            }
           
            returnthis.SUCCESS;
        }else{
            msg = "登录失败,用户名或密码错";
            returnthis.ERROR;
        }
    }
   
    public String regist() throws Exception{
        //将用户名,密码添加到数据库
        //...
        msg = "注册成功。";
        returnthis.SUCCESS;
    }
}

 

2.       struts.
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="my" extends="struts-default" namespace="/manage">
    <!-- 定义处理请求URL为login.action的Action -->
        <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction">
        <!-- 定义处理结果字符串和资源之间的映射关系 -->
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

 

3.       页面:
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
    <title>用户登录页面</title>
</head>
 
<body>
  <h2>用户入口</h2>
  <hr>
    <form action="manage/userOpt!login.action" method="post">
    <table border="1">
         <tr>
             <td>用户名:</td>
             <td><input type="text" name="userName"/></td>
         </tr>
         <tr>
             <td>密码:</td>
             <td><input type="password" name="password"/></td>
         </tr>
         <tr>
             <td colspan="2">
                 <input type="submit" value=" 确定 "/>
             </td>
         </tr>
    </table>
    </form>
</body>
</html>

 

regist.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
    <title>用户注册页面</title>
</head>
 
<body>
  <h2>用户注册</h2>
  <hr>
    <form action="manage/userOpt!regist.action" method="post">
    <table border="1">
         <tr>
             <td>用户名:</td>
             <td><input type="text" name="userName"/></td>
         </tr>
         <tr>
             <td>密码:</td>
             <td><input type="password" name="password"/></td>
         </tr>
         <tr>
             <td colspan="2">
                 <input type="submit" value=" 注册 "/>
             </td>
         </tr>
    </table>
    </form>
</body>
</html>

现在就可以使用sturts。
  



原标题:sturuts配置

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

ldGlobalService:https://www.ikjzd.com/w/1573202106702880769
零零赶跟卖:https://www.ikjzd.com/w/1573202107386552322
Lead Ninja:https://www.ikjzd.com/w/1573202119054761986
Leadslide Coupon Popup:https://www.ikjzd.com/w/1573202126147674113
力谱宿云:https://www.ikjzd.com/w/1573202127070076930
乐都特(La Redoute):https://www.ikjzd.com/w/1573202128793935873
价格翻了8倍,居然还有人买,秘诀在这里。:https://www.kjdsnews.com/a/1836642.html
欧洲B2B电商市场有待挖掘!销售总额将达1.7万亿欧元:https://www.kjdsnews.com/a/1836643.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流