Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC互动程序_Java_Spring Mvc - Fatal编程技术网

Java Spring MVC互动程序

Java Spring MVC互动程序,java,spring-mvc,Java,Spring Mvc,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="ht

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>
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);
    }
}
web.xml
SpringMVC应用程序
/WEB-INF/index.jsp
mvc调度器
org.springframework.web.servlet.DispatcherServlet
1.
mvc调度器
/
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/mvc dispatcher servlet.xml,/WEB-INF/spring-security.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
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>

org.springframework.web.servlet.view.tiles2.TilesView
/WEB-INF/tiles.xml
/WEB-INF/pages/
.jsp
我的信息
com.e3learning.model.Account
${hibernate.dial}
${hibernate.show_sql}
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

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>
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);
    }
}
package com.e3learning.controller;
导入java.util.HashMap;
导入java.util.Map;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.validation.BindingResult;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.servlet.ModelAndView;
导入com.e3learning.model.Account;
导入com.e3learning.model.Contact;
导入com.e3learning.service.AccountService;
导入com.e3learning.service.AccountServiceImpl;
@控制器
公共类帐户控制器{
@自动连线
私人帐户服务;
@RequestMapping(value=“/login”,method=RequestMethod.GET)
公共字符串登录(ModelMap模型){
返回“登录”;
}
@RequestMapping(value=“/loginfailed”,method=RequestMethod.GET)
公共字符串登录错误(ModelMap模型){
model.addAttribute(“错误”、“真实”);
返回“登录”;
}
@RequestMapping(value=“/logout”,method=RequestMethod.GET)
公共字符串注销(ModelMap模型){
返回“登录”;
}
@RequestMapping(value=“/save”,method=RequestMethod.POST)
公共模型和视图保存帐户(@modeldattribute(“帐户”)帐户,BindingResult){
accountService.addAccount(account);
映射模型=新的HashMap();
model.put(“accounts”,accountService.listcounts());
返回新模型和视图(“accountlist”,模型);
}
@RequestMapping(value=“/accountadd”,method=RequestMethod.GET)
公共模型和视图添加帐户(@modeldattribute(“帐户”)帐户,BindingResult){
返回新的ModelAndView(“accountadd”);
}
@RequestMapping(value=“/accountdelete/{accountid}”,method=RequestMethod.GET)
公共模型和视图删除帐户(@PathVariable String accountid){
accountService.deleteCount(accountid);
映射模型=新的HashMap();
model.put(“accounts”,accountService.listcounts());
返回新模型和视图(“accountlist”,模型);
}
/*@RequestMapping(value=“/accountedit”,method=RequestMethod.GET)
公共模型和视图编辑帐户(@modeldattribute(“帐户”)帐户,BindingResult){
返回新的ModelAndView(“accountedit”);
}*/
@请求映射(“/welcome”)
公共模型和视图showWelcome(){
System.out.println(“欢迎天才”);
返回新模型和视图(“欢迎”);
}
@请求映射(“/mylist”)
公共模型和视图显示列表(){
System.out.println(“你好列表”);
返回新的ModelAndView(“mylist”);
}
@RequestMapping(value=“/accountlist”,method=RequestMethod.GET)
公共模型和视图列表帐户(){
映射模型=新的HashMap();
model.put(“accounts”,accountService.listcounts());
返回新模型和视图(“accountlist”,模型);
}
@请求映射(“/contact”)
公共模型和视图显示联系人(){
System.out.println(“你好,天才”);
返回新的ModelAndView(“联系人”、“命令”、新联系人());
}
@RequestMapping(value=“/accountedit/{accountid}”,method=RequestMethod.GET)
公共模型和视图更新计数(@PathVariable String accountid){
System.out.println(“你好更新”);
返回新的ModelAndView(“accountupdate”、“account”、accountService.getAccountDetails(accountid));
//返回新的ModelAndView(“accountupdate”,accountService.getAccountDetails(accountid));
}
@RequestMapping(value=“/accountupdate”,method=RequestMethod.POST)
公共模型和视图编辑帐户(@modeldattribute(“帐户”)帐户,BindingResult){
accountService.updateAccount(account);
映射模型=新的HashMap();
model.put(“accounts”,accountService.listcounts());
返回新模型和视图(“accountlist”,模型);
}
}
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>

列出帐户
列出帐户