Beego:显示从表单输入字段中获取的已登录用户名。

huangapple go评论88阅读模式
英文:

Beego:Display logged in username taken from input field of form

问题

我正在使用Beego框架,我有两个HTML视图文件,一个是登录页面(login.html),另一个是主页(landing.html)。

login.html的代码如下:

<form action="/commonDashboard" name="loginForm" method="post" autocomplete="off" id="loginForm">
    <input type="text" placeholder="请输入用户名" id="userName" name="userName" taborder="1">
    <input type="password" class="qwerty" placeholder="请输入密码" id="userPassword" name="userPassword" taborder="2">
    <input type="button" value="登录" onclick="validatelogin();" taborder="3">
    <input type="button" value="清除" onclick="resetForm();" taborder="3">
    <span class="forgot"><a href="#">忘记密码?</a></span>
    <!-- 虚拟键盘 -->
    <span class="virtual"><a href="#" id="passwd" class="tooltip-tipsy" title="点击打开虚拟键盘">虚拟键盘</a></span>
</form>

landing.html的代码如下:

<header class="top">
    <div class="container-fluid">
        <aside class="logo"><img src="img.jpg" class="img-responsive"></aside>
        <aside class="top-right">
            <div class="profile">
                <ul>
                    <li class="name"><img src="../static/img/profile-img.jpg" alt="Shri Ram"> 你好 <a href="#">Shri</a></li>
                    <li class="logout"><a href="/login">退出</a></li>
                    <li><a href="#">联系人</a></li>
                    <li class="last"><a href="#">帮助</a></li>
                </ul>
            </div>
            <div class="login-time">
                <ul>
                    <li><span>当前登录:</span> Mon 22 Sep 2014 03:28 PM</li>
                    <li class="last"><span>上次登录:</span> Sun 21 Sep 2014 02:28 PM</li>
                </ul>
            </div>
        </aside>
    </div>
</header>

控制器的代码如下:

package controllers

import (
    "fmt"
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (c *MainController) Get() {
    c.TplNames = "login.html"
    fmt.Println("login Get Method")
}

type LoginController struct {
    beego.Controller
}

func (c *LoginController) Post() {
    c.TplNames = "commonDashboard.html"
    fmt.Println("this is dashboard")
    password := c.Input().Get("userPassword")
    username := c.GetSession("userName").(string)
    c.SetSession("userName", username)
    c.Data["user"] = username
    fmt.Println("password:" + " " + password)
}

type HeaderController struct {
    beego.Controller
}

func (c *HeaderController) Get() {
    fmt.Println("this is Header")
    username := c.Input().Get("userName")
    fmt.Println(username)
    c.TplNames = "commonHeader.html"
}

type FooterController struct {
    beego.Controller
}

func (c *FooterController) Get() {
    fmt.Println("this is footer")
    c.TplNames = "commonFooter.html"
}

type MenuController struct {
    beego.Controller
}

func (c *MenuController) Get() {
    fmt.Println("this is menu")
    c.TplNames = "commonMenu.html"
}

你想在HTML文件的页眉(header)中显示当前登录的用户名。

英文:

I am using Beego framework for my purpose and i have two html view files one is login and other is landing
login.html

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

&lt;form action=&quot;/commonDashboard&quot; name=&quot;loginForm&quot; method=&quot;post&quot; autocomplete=&quot;off&quot; id=&quot;loginForm&quot; &gt;
&lt;input type=&quot;text&quot; placeholder=&quot;Enter your user name&quot; id=&quot;userName&quot; name=&quot;userName&quot; taborder=&quot;1&quot;&gt;
&lt;input type=&quot;password&quot; class=&quot;qwerty&quot; placeholder=&quot;Enter your password&quot; id=&quot;userPassword&quot; name=&quot;userPassword&quot; taborder=&quot;2&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Login&quot; onclick=&quot;validatelogin();&quot; taborder=&quot;3&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Clear&quot; onclick=&quot;resetForm();&quot; taborder=&quot;3&quot;&gt;
&lt;span class=&quot;forgot&quot;&gt;&lt;a href=&quot;#&quot;&gt;forgot password ?&lt;/a&gt;&lt;/span&gt;
&lt;!-- Virtual Keyboard --&gt;
&lt;span class=&quot;virtual&quot;&gt;&lt;a href=&quot;#&quot; id=&quot;passwd&quot; class=&quot;tooltip-tipsy&quot; title=&quot;Click to open the virtual keyboard&quot;&gt;Virtual Keyboard  
&lt;/span&gt;
&lt;/form&gt;

<!-- end snippet -->

and landing page is

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

&lt;header class=&quot;top&quot;&gt;
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;aside class=&quot;logo&quot;&gt;&lt;img src=&quot;img.jpg&quot;  class=&quot;img-responsive&quot;&gt;&lt;/aside&gt;
&lt;aside class=&quot;top-right&quot;&gt;
&lt;div class=&quot;profile&quot;&gt;
&lt;ul&gt;
&lt;li class=&quot;name&quot;&gt;&lt;img src=&quot;../static/img/profile-img.jpg&quot; alt=&quot;Shri Ram&quot;&gt; Hi &lt;a href=&quot;#&quot;&gt;Shri&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;logout&quot;&gt;&lt;a href=&quot;/login&quot;&gt;Logout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contacts&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;last&quot;&gt;&lt;a href=&quot;#&quot;&gt;Help&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class=&quot;login-time&quot;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Current Login:&lt;/span&gt; Mon 22 Sep 2014 03:28 PM&lt;/li&gt;
&lt;li class=&quot;last&quot;&gt;&lt;span&gt;Last Login:&lt;/span&gt; Sun 21 Sep 2014 02:28 PM&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/aside&gt;
&lt;/div&gt;
&lt;/header&gt;

<!-- end snippet -->

and controller is as follows

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

package controllers
import (
&quot;fmt&quot;
&quot;github.com/astaxie/beego&quot;
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.TplNames = &quot;login.html&quot;
fmt.Println(&quot;login Get Method&quot;)
}
type LoginController struct {
beego.Controller
}
func (c *LoginController) Post() {
c.TplNames = &quot;commonDashboard.html&quot;
fmt.Println(&quot;this is dashboard&quot;)
password := c.Input().Get(&quot;userPassword&quot;)
username := c.GetSession(&quot;userName&quot;).(string)
c.SetSession(&quot;userName&quot;, username)
c.Data[&quot;user&quot;] = username
fmt.Println(&quot;password:&quot; + &quot; &quot; + password)
}
type HeaderController struct {
beego.Controller
}
func (c *HeaderController) Get() {
fmt.Println(&quot;this is Header&quot;)
username := c.Input().Get(&quot;userName&quot;)
fmt.Println(username)
c.TplNames = &quot;commonHeader.html&quot;
}
type FooterController struct {
beego.Controller
}
func (c *FooterController) Get() {
fmt.Println(&quot;this is footer&quot;)
c.TplNames = &quot;commonFooter.html&quot;
}
type MenuController struct {
beego.Controller
}
func (c *MenuController) Get() {
fmt.Println(&quot;this is menu&quot;)
c.TplNames = &quot;commonMenu.html&quot;
}

<!-- end snippet -->

i want to display the currently logged in username in header of html file

答案1

得分: 1

app.conf中启用session支持:

sessionon = true

如果登录有效,将用户名保存到session中:

func (c *LoginController) Post() {
    var json JsonObject
    u := &User{}
    if err := c.ParseForm(u); err != nil {
        json = JsonObject{
            Success: false,
            Message: "服务器错误",
        }
    } else {
        if u.Username == "admin" && u.Password == "123456" {
            json = JsonObject{
                Success: true,
            }
            // 登录成功,将用户放入session中,你可以只放入用户名
            c.SetSession("User", u)
        } else {
            json = JsonObject{
                Success: false,
                Message: "用户名或密码不正确",
            }
        }
    }
    c.Data["json"] = json
    c.ServeJson()
}

然后你可以在每个Controller中获取用户名:

func (c *IndexController) Get() {
    // 从session中获取用户,你可以只获取用户名
    u := c.GetSession("User")
    if u == nil {
        c.Redirect("/login", 302)
    }
    // 将session中的用户设置到响应数据中
    c.Data["User"] = u
    c.TplNames = "index.html"
}

在模板中以模板格式获取它:

{{ .User.username }}

你也可以只使用{{ .userName }}

英文:

Enable session support in app.conf with:

sessionon = true

Save the username into session if login is valid:

func (c *LoginController) Post() {
var json JsonObject
u := &amp;User{}
if err := c.ParseForm(u); err != nil {
json = JsonObject{
Success: false,
Message: &quot;Server error&quot;,
}
} else {
if u.Username == &quot;admin&quot; &amp;&amp; u.Password == &quot;123456&quot; {
json = JsonObject{
Success: true,
}
// successfully login, put the user into session, you may put just username in
c.SetSession(&quot;User&quot;, u)
} else {
json = JsonObject{
Success: false,
Message: &quot;Incorrect username or password&quot;,
}
}
}
c.Data[&quot;json&quot;] = json
c.ServeJson()
}

Then you can recover the username in every Controller:

func (c *IndexController) Get() {
// get user from session, you may get just username
u := c.GetSession(&quot;User&quot;)
if u == nil {
c.Redirect(&quot;/login&quot;, 302)
}
// set user from session into response data
c.Data[&quot;User&quot;] = u
c.TplNames = &quot;index.html&quot;
}

Get it on the html with template format:

{{ .User.username }}

You can just use {{ .userName }}

huangapple
  • 本文由 发表于 2015年5月26日 21:00:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/30459311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定