从控制器返回新的Thymeleaf页面。

huangapple 未分类评论46阅读模式
英文:

Return new thymeleaf page from controller

问题

<!-- In Thymeleaf index page -->
<div class="navbar-text navbar-right">
    <div class="btn-group dropdown" style="color:#003d71;">
        <span id="menu1" class="fa fa-align-justify dropdown-toggle hoverIcon" data-toggle="dropdown"></span>
        <ul class="dropdown-menu" role="menu" aria-labelledby="menu1" style="background:#003d71;">
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#" data-toggle="modal" data-target="#changePasswordModal" data-backdrop="false" data-keyboard="false">Change Password</a></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#" data-toggle="modal" data-target="#whatsNewModal" data-backdrop="false" data-keyboard="false">What's New</a></li>
            <li role="presentation" sec:authorize="isAuthenticated()">
                <a role="menuitem" tabindex="-1" th:href="@{/logout}">Sign Out</a>
            </li>
            <li><a role="menuitem" href="#" th:onclick="'loadAboutUs()'">About Us</a></li>
        </ul>
    </div>
</div>
// In javascript
function loadAboutUs() {
    $.ajax({
        type: "GET",
        url: "/app/aboutUs/",
        cache: false,
        timeout: 100000,
        success: function (result) {
            console.log("Success", result);
            $("#mainBody").html(result);
            // Trying below takes me to logout page
            window.location.href = "/app/aboutUs";
        },
        error: function (e) {
            window.location = "/app/login";
            console.log("ERROR: ", e);
        },
        done: function (e) {
            console.log("DONE");
        },
    });
}
// In my controller
@RequestMapping(value = "/aboutUs", method = RequestMethod.GET)
public ModelAndView aboutUs(HttpServletRequest request, Model model, HttpServletResponse response) throws Exception {
    session = request.getSession(false);
    return new ModelAndView("th_aboutUs");
}
// In SecurityConfiguration file
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
        .antMatchers("/", "/images/**", "/login**", "/callback/", "/**/*.js", "/**/*.css", "/**/*.scss", "/**/*.map");
}

@Bean
public FilterRegistrationBean<SessionFilter> loggingFilter(){
    FilterRegistrationBean<SessionFilter> registrationBean = new FilterRegistrationBean<>();

    registrationBean.setFilter(new SessionFilter());
    registrationBean.addUrlPatterns("/index/*");
    registrationBean.addUrlPatterns("/aboutUs/*");

    return registrationBean;
}
英文:

I have a web app built using Spring boot and thymeleaf. I have used only the index page so far which has different models in it and it work just fine.

Now I want to add a new page. By new page I mean in the url it says
http://localhost:8081/app/index

Now I have added a dropdown on the page Let's say About Us. When I try to do that it stays on that page and don't do much or it takes me to logout page, here is my code:

In Thymeleaf index page:

       &lt;div class=&quot;navbar-text navbar-right&quot;&gt;
        &lt;div class=&quot;btn-group dropdown&quot; style=&quot;color:#003d71;&quot;&gt;
            &lt;span id=&quot;menu1&quot; class=&quot;fa fa-align-justify dropdown-toggle hoverIcon&quot; data- 
            toggle=&quot;dropdown&quot;&gt;&lt;/span&gt;
            &lt;ul class=&quot;dropdown-menu&quot; role=&quot;menu&quot; aria-labelledby=&quot;menu1&quot; 
            style=&quot;background:#003d71;&quot;&gt;

                &lt;li role=&quot;presentation&quot;&gt;&lt;a role=&quot;menuitem&quot; tabindex=&quot;-1&quot; href=&quot;#&quot; data- 
                toggle=&quot;modal&quot; data-target=&quot;#changePasswordModal&quot; data-backdrop=&quot;false&quot; 
                data-keyboard=&quot;false&quot;&gt;Change Password&lt;/a&gt;&lt;/li&gt;
                &lt;li role=&quot;presentation&quot;&gt;&lt;a role=&quot;menuitem&quot; tabindex=&quot;-1&quot; href=&quot;#&quot; data- 
                 toggle=&quot;modal&quot; data-target=&quot;#whatsNewModal&quot; data-backdrop=&quot;false&quot; data- 
                 keyboard=&quot;false&quot;&gt;What&#39;s New&lt;/a&gt;&lt;/li&gt;
                &lt;li role=&quot;presentation&quot; sec:authorize=&quot;isAuthenticated()&quot;&gt;
                    &lt;a role=&quot;menuitem&quot; tabindex=&quot;-1&quot; th:href=&quot;@{/logout}&quot;&gt;Sign Out&lt;/a&gt;
                &lt;/li&gt;
                &lt;li&gt;&lt;a role=&quot;menuitem&quot; href=&quot;#&quot; th:onclick=&quot;&#39;loadAboutUs()&#39;&quot;&gt;About Us&lt;/a&gt; 
                &lt;/li&gt;
                &lt;!-- &lt;li&gt;&lt;a role=&quot;menuitem&quot; th:href=&quot;@{th_aboutUs.html}&quot;&gt;About Us&lt;/a&gt;&lt;/li&gt;-- 
                  &gt;
                &lt;!-- Using above taking to logout--&gt;
            &lt;/ul&gt;
        &lt;/div&gt;

In javascript:

  function loadAboutUs() {
  $.ajax({
    type: &quot;GET&quot;,
    url: &quot;/app/aboutUs/&quot;,
    cache: false,
    timeout: 100000,
    success: function (result) {
     console.log(&quot;Success&quot;,result);
         $(&quot;#mainBody&quot;).html(result);

    // Trying below takes me to logout page
        window.location.href = &quot;/app/aboutUs&quot;;

    },
    error: function (e) {
        window.location = &quot;/app/login&quot;;
        console.log(&quot;ERROR: &quot;, e);
    },
    done: function (e) {
        console.log(&quot;DONE&quot;);
    },
   });
  }

In my controller:

  @RequestMapping(value = &quot;/aboutUs&quot;, method = RequestMethod.GET)
 public ModelAndView aboutUs(HttpServletRequest request, Model model, HttpServletResponse 
  response) throws Exception {
    session = request.getSession(false);
    return new ModelAndView(&quot;th_aboutUs&quot;);
  }

I have a SecurityConfiguration file which has the below function and I added the Url pattern in it if in case it is effecting it:

     @Override
     public void configure(WebSecurity web) throws Exception {
        web.ignoring()
            .antMatchers(&quot;/&quot;,&quot;/images/**&quot;, 
         &quot;/login**&quot;,&quot;/callback/&quot;,&quot;/**/*.js&quot;,&quot;/**/*.css&quot;,&quot;/**/*.scss&quot;,&quot;/**/*.map&quot;);
      }

    @Bean
public FilterRegistrationBean&lt;SessionFilter&gt; loggingFilter(){
    FilterRegistrationBean&lt;SessionFilter&gt; registrationBean
            = new FilterRegistrationBean&lt;&gt;();

    registrationBean.setFilter(new SessionFilter());
    registrationBean.addUrlPatterns(&quot;/index/*&quot;);
    registrationBean.addUrlPatterns(&quot;/aboutUs/*&quot;);

    return registrationBean;
}

Would somebody please guide me what is going on?

huangapple
  • 本文由 发表于 2020年4月9日 11:27:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/61113466.html
匿名

发表评论

匿名网友

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

确定