简体   繁体   English

Java Spring MVC Tile

[英]Java Spring MVC Tile

Java 爪哇

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <display-name>Spring MVC Application</display-name>
    <welcome-file-list>
        <welcome-file>/WEB-INF/index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml,/WEB-INF/spring-security.xml</param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

mvc-dispatcher-servlet.xml mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />
    <context:component-scan base-package="com.e3learning" />
    <context:component-scan base-package="com.e3learning.controller" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <bean id="viewResolver1" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.tiles2.TilesView</value>
        </property>
    </bean>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.e3learning.model.Account</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>             
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

tiles.xml Tiles.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="base.definition" template="/WEB-INF/pages/jsp/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/pages/jsp/header.jsp" />
        <put-attribute name="menu" value="/WEB-INF/pages/jsp/menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/pages/jsp/footer.jsp" />
    </definition>

    <definition name="welcome" extends="base.definition">
        <put-attribute name="title" value="Welcome To E3 Learning" />
        <put-attribute name="body" value="/WEB-INF/pages/welcome.jsp" />
    </definition>

    <definition name="login" extends="base.definition">
        <put-attribute name="title" value="Login E3 Learning" />
        <put-attribute name="body" value="/WEB-INF/pages/login.jsp" />
    </definition>

    <definition name="contact" extends="base.definition">
        <put-attribute name="title" value="Contact E3 Learning" />
        <put-attribute name="body" value="/WEB-INF/pages/contact.jsp" />
    </definition>

    <definition name="mylist" extends="base.definition">
        <put-attribute name="title" value="My List E3 Learning" />
        <put-attribute name="body" value="/WEB-INF/pages/mylist.jsp" />
    </definition>

    <definition name="accountlist" extends="base.definition">
        <put-attribute name="title" value="Account List E3 Learning" />
        <put-attribute name="body" value="/WEB-INF/pages/accountlist.jsp" />
    </definition>

    <definition name="accountedit" extends="base.definition">
        <put-attribute name="title" value="Account List E3 Learning" />
        <put-attribute name="body" value="/WEB-INF/pages/accountupdate.jsp" />
    </definition>

    <definition name="accountadd" extends="base.definition">
        <put-attribute name="title" value="Add Account E3 Learning" />
        <put-attribute name="body" value="/WEB-INF/pages/accountadd.jsp" />
    </definition>

</tiles-definitions>

AccountController.java AccountController.java

package com.e3learning.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.e3learning.model.Account;
import com.e3learning.model.Contact;
import com.e3learning.service.AccountService;
import com.e3learning.service.AccountServiceImpl;

@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(ModelMap model) {

        return "login";
    }

    @RequestMapping(value="/loginfailed", method = RequestMethod.GET)
    public String loginerror(ModelMap model) {
        model.addAttribute("error", "true");
        return "login";
    }

    @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {
        return "login";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveAccount(@ModelAttribute("account") Account account, BindingResult result) {
        accountService.addAccount(account);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("accounts", accountService.listAccounts());

        return new ModelAndView("accountlist", model);
    }

    @RequestMapping(value = "/accountadd", method = RequestMethod.GET)
    public ModelAndView addAccount(@ModelAttribute("account") Account account, BindingResult result) {

        return new ModelAndView("accountadd");
    }

    @RequestMapping(value = "/accountdelete/{accountid}", method = RequestMethod.GET)
    public ModelAndView deleteAccount(@PathVariable String accountid) {
        accountService.deleteAccount(accountid);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("accounts", accountService.listAccounts());

        return new ModelAndView("accountlist", model);
    }

    /*@RequestMapping(value = "/accountedit", method = RequestMethod.GET)
    public ModelAndView editAccount(@ModelAttribute("account") Account account, BindingResult result) {

        return new ModelAndView("accountedit");
    }*/

    @RequestMapping("/welcome")
    public ModelAndView showWelcome() {
        System.out.println("## WELCOME GENIUS");
        return new ModelAndView("welcome");
    }

    @RequestMapping("/mylist")
    public ModelAndView showList() {
        System.out.println("## HELLO LIST");
        return new ModelAndView("mylist");
    }

    @RequestMapping(value = "/accountlist", method = RequestMethod.GET)
    public ModelAndView listAccounts() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("accounts", accountService.listAccounts());

        return new ModelAndView("accountlist", model);
    }

    @RequestMapping("/contact")
    public ModelAndView showContacts() {
        System.out.println("## HELLO GENIUS");
        return new ModelAndView("contact", "command", new Contact());
    }

    @RequestMapping(value = "/accountedit/{accountid}", method = RequestMethod.GET)
    public ModelAndView updateAccount(@PathVariable String accountid) {
        System.out.println("## HELLO UPDATE");
        return new ModelAndView("accountupdate", "account", accountService.getAccountDetails(accountid));
        //return new ModelAndView("accountupdate", accountService.getAccountDetails(accountid));
    }

    @RequestMapping(value = "/accountupdate", method = RequestMethod.POST)
    public ModelAndView editAccount(@ModelAttribute("account") Account account, BindingResult result) {

        accountService.updateAccount(account);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("accounts", accountService.listAccounts());

        return new ModelAndView("accountlist", model);
    }
}

accountList.jsp accountList.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>List Accounts</title>
    </head>
    <body>
        <h1>List Accounts</h1>
        <!-- <a href="/SecurityNamespace/add">Add Account</a> -->
        <form:form modelAttribute="account" method="GET" >
            <table border="1" cellpadding="2" cellspacing="5" width="100%">
                <tr>
                    <th>Account ID</th>
                    <th>Account Name</th>
                    <th>Account Description</th>
                    <th>Account Creation Date</th>
                    <th>Update</th>
                    <th>Delete</th>
                </tr>
                <c:if test="${!empty accounts}">
                    <c:forEach items="${accounts}" var="account">
                        <tr>
                            <td><c:out value="${account.accountId}"/></td>
                            <td><c:out value="${account.accountName}"/></td>
                            <td><c:out value="${account.accountDescription}"/></td>
                            <td><c:out value="${account.accountCreationDate}"/></td>
                            <td><a href="/SecurityNamespace/accountedit/${account.accountId}">Update</a></td>
                            <td><a href="/SecurityNamespace/accountdelete/${account.accountId}">Delete</a></td>
                        </tr>
                    </c:forEach>
                </c:if>
            </table>
        </form:form>
    </body>
</html>

I am using spring tiles 2 for common layout.I want to get my page with id through tiles .When i open my page for edit this link contain path with id then its open in new page instead of common layout.Please help me 我正在使用Spring Tiles 2进行通用布局。我想通过Tiles获得具有ID的页面。当我打开页面进行编辑时,此链接包含具有ID的路径,然后在新页面中打开它而不是通用布局。请帮助我

LIKE: THIS JAVA 像:这JAVA

It's kind of hard to decipher what you are asking, but I'll give it a go. 很难理解您的要求,但我会尝试一下。 If you're talking about accountedit/<id> page, then I'm guessing this call is wrong: 如果您谈论的是accountedit/<id>页面,那么我猜这个电话是错误的:

@RequestMapping(value = "/accountedit/{accountid}", method = RequestMethod.GET)
public ModelAndView updateAccount(@PathVariable String accountid) {
    System.out.println("## HELLO UPDATE");
    return new ModelAndView("accountupdate", "account", accountService.getAccountDetails(accountid));
    //return new ModelAndView("accountupdate", accountService.getAccountDetails(accountid));
}

I'd try and change it to 我会尝试将其更改为

@RequestMapping(value = "/accountedit/{accountid}", method = RequestMethod.GET)
public ModelAndView updateAccount(@PathVariable String accountid) {
    System.out.println("## HELLO UPDATE");
    return new ModelAndView("accountedit", "account", accountService.getAccountDetails(accountid));
}

I think you have problems as accountupdate is not a mapped tile. 我认为您有问题,因为accountupdate不是映射图块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM