JSP没有绑定控制器中bean的元素

时间:2017-07-04 16:04:02

标签: spring jsp spring-mvc javabeans jsp-tags

问题是当我点击JSP页面上的提交按钮时,它会转到Controller但它没有将JSP元素绑定到Bean类元素。 在控制器中,我得到bean的对象

stock.jsp将动态添加到index.jsp页面

的index.jsp

<!DOCTYPE html>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Metalist Enterprises</title>
<link href="resource/vendor/bootstrap/css/bootstrap.min.css"
    rel="stylesheet">
<link href="resource/vendor/metisMenu/metisMenu.min.css"
    rel="stylesheet">
<link href="resource/dist/css/sb-admin-2.css" rel="stylesheet">
<link href="resource/vendor/morrisjs/morris.css" rel="stylesheet">
<link href="resource/vendor/font-awesome/css/font-awesome.min.css"
    rel="stylesheet" type="text/css">
<script src="resource/vendor/jquery/jquery.min.js"></script>
<script>
    function ajaxMethod(referPageName) {
        var urlName = '';
        if (referPageName == 'newInvoice')
            urlName = referPageName;
        else if (referPageName == 'inward')
            urlName = referPageName;
        else if (referPageName == 'outward')
            urlName = referPageName;
        else if (referPageName == 'search')
            urlName = referPageName;
        else
            urlName = 'stock';
        $.ajax({
            url : urlName,
            success : function(responseText) {
                $('#page-wrapper').html(responseText);
            }
        });
    }
</script>
<script>
    $(document).on('click', 'input[name="divOptionsRadios"]', function() {
        var varInwardStock = document.getElementById('inwardStock');
        var varOutwardStock = document.getElementById('outwardStock');
        if (varInwardStock.style.display === 'none') {
            varInwardStock.style.display = 'block';
            varOutwardStock.style.display = 'none';
        } else {
            varInwardStock.style.display = 'none';
            varOutwardStock.style.display = 'block';
        }

    });
</script>
</head>
<body>
    <div id="wrapper">
        <nav class="navbar navbar-default navbar-static-top" role="navigation"
            style="margin-bottom: 0">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span> <span
                        class="icon-bar"></span> <span class="icon-bar"></span> <span
                        class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="index.html">Metalist Enterprises</a>
            </div>
            <div class="navbar-default sidebar" role="navigation">
                <div class="sidebar-nav navbar-collapse">
                    <ul class="nav" id="side-menu">
                        <li class="sidebar-search">
                            <div class="input-group custom-search-form">
                                <input type="text" class="form-control" placeholder="Search...">
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">
                                        <i class="fa fa-search"></i>
                                    </button>
                                </span>
                            </div> <!-- /input-group -->
                        </li>
                        <li><a href="home"><i class="fa fa-dashboard fa-fw"></i>
                                Dashboard</a></li>
                        <li onclick="ajaxMethod('newInvoice')"><a href="#"><i
                                class="fa fa-table fa-fw"></i> New Invoice</a></li>
                        <li onclick="ajaxMethod('inward')"><a href="#"><i
                                class="fa fa-edit fa-fw"></i> Inward</a></li>
                        <li onclick="ajaxMethod('outward')"><a href="#"><i
                                class="fa fa-wrench fa-fw"></i> Outward</a></li>
                        <li onclick="ajaxMethod('search')"><a href="#"><i
                                class="fa fa-sitemap fa-fw"></i> Search</a></li>
                        <li onclick="ajaxMethod('stock');"><a href="#"><i
                                class="fa fa-files fa-fw"></i> Add Stock</a></li>
                    </ul>
                </div>
                <!-- /.sidebar-collapse -->
            </div>
        </nav>
    </div>
    <script src="resource/vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="resource/vendor/metisMenu/metisMenu.min.js"></script>
    <script src="resource/vendor/raphael/raphael.min.js"></script>
    <script src="resource/vendor/morrisjs/morris.min.js"></script>
    <script src="resource/data/morris-data.js"></script>
    <script src="resource/dist/js/sb-admin-2.js"></script>
</body>
</html>

stock.jsp

<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">Stock</h1>
    </div>
    <div class="col-lg-6">
        <div class="radio">
            <label> <input type="radio" name="divOptionsRadios"
                id="divOptionsRadios1" value="inward" checked='checked'>Inward
            </label>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="radio">
            <label> <input type="radio" name="divOptionsRadios"
                id="divOptionsRadios2" value="outward">Outward
            </label>
        </div>
    </div>
</div>
<!-- /.row -->
<div class="row">
    <div class="col-lg-12">
        <div class="form-group">
            <div class="panel panel-default" id="inwardStock">
                <!-- style="display: none;"> -->
                <div class="panel-heading">Inward Stock</div>
                <div class="panel-body">
                    <form action="saveStock" method="post">
                        <div class="row">
                            <div class="col-lg-4">
                                <div class="form-group">
                                    <label>Invoice</label> <input class="form-control"
                                        name="invoiceNumber" placeholder="Invoice">
                                </div>
                            </div>
                            <div class="col-lg-4">
                                <div class="form-group input-group">
                                    <label>Date of Invoice</label><input type="text"
                                        name="invoiceDate" class="form-control"
                                        placeholder="Please select Date"> <span
                                        class="input-group-addon">.00</span>
                                </div>
                            </div>
                            <div class="col-lg-4">
                                <div class="form-group">
                                    <label>Name Of Company</label> <select class="form-control"
                                        name="companyName">
                                        <option>1</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                        <option>5</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="form-group">
                                    <label>Material Description</label>
                                    <textarea class="form-control" rows="1"
                                        name="materialDescription" placeholder="Description"></textarea>
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="form-group">
                                    <label>Quantity</label> <input class="form-control"
                                        path="quantity" placeholder="Quantity">
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="form-group">
                                    <label>Rate</label> <input class="form-control" name="rate"
                                        placeholder="Rate">
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="form-group">
                                    <label>Tax</label> <input class="form-control" name="tax"
                                        placeholder="Tax">
                                </div>
                            </div>
                            <div class="col-lg-3">
                                <div class="form-group">
                                    <label>Amount</label> <input class="form-control" name="amount"
                                        placeholder="Amount">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4"></div>
                            <div class="col-lg-4">
                                <button type="submit" class="btn btn-default">Save</button>
                                <button type="reset" class="btn btn-default">Reset</button>
                            </div>
                            <div class="col-lg-4"></div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="panel panel-default" id="outwardStock"
                style="display: none;">
                <div class="panel-heading">Outward Stock</div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-12">
                            <label>Please select</label>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="radio">
                                <label> <input type="radio" name="optionsRadios"
                                    id="optionsRadios1" value="option1" checked>Inward
                                </label>
                            </div>
                        </div>
                        <div class="col-lg-6">
                            <div class="radio">
                                <label> <input type="radio" name="optionsRadios"
                                    id="optionsRadios2" value="option2">Outward
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-6">
                        <form role="form">
                            <div class="form-group">
                                <label>Please select</label>
                                <div class="radio">
                                    <label> <input type="radio" name="optionsRadios"
                                        id="optionsRadios3" value="option1" checked>Inward
                                    </label>
                                </div>
                                <div class="radio">
                                    <label> <input type="radio" name="optionsRadios"
                                        id="optionsRadios4" value="option2">Outward
                                    </label>
                                </div>
                                <!-- <div class="radio">
                                    <label> <input type="radio" name="optionsRadios"
                                        id="optionsRadios5" value="option3">Radio 3
                                    </label>
                                </div> -->
                            </div>
                            <div class="form-group">
                                <label>Text Input</label> <input class="form-control">
                                <p class="help-block">Example block-level help text here.</p>
                            </div>
                            <div class="form-group">
                                <label>Text Input with Placeholder</label> <input
                                    class="form-control" placeholder="Enter text">
                            </div>
                            <div class="form-group">
                                <label>Static Control</label>
                                <p class="form-control-static">email@example.com</p>
                            </div>
                            <div class="form-group">
                                <label>File input</label> <input type="file">
                            </div>
                            <div class="form-group">
                                <label>Text area</label>
                                <textarea class="form-control" rows="3"></textarea>
                            </div>
                            <div class="form-group">
                                <label>Checkboxes</label>
                                <div class="checkbox">
                                    <label> <input type="checkbox" value="">Checkbox
                                        1
                                    </label>
                                </div>
                                <div class="checkbox">
                                    <label> <input type="checkbox" value="">Checkbox
                                        2
                                    </label>
                                </div>
                                <div class="checkbox">
                                    <label> <input type="checkbox" value="">Checkbox
                                        3
                                    </label>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Inline Checkboxes</label> <label class="checkbox-inline">
                                    <input type="checkbox">1
                                </label> <label class="checkbox-inline"> <input type="checkbox">2
                                </label> <label class="checkbox-inline"> <input type="checkbox">3
                                </label>
                            </div>
                            <div class="form-group">
                                <label>Radio Buttons</label>
                                <div class="radio">
                                    <label> <input type="radio" name="optionsRadios"
                                        id="optionsRadios11" value="option1" checked>Radio 1
                                    </label>
                                </div>
                                <div class="radio">
                                    <label> <input type="radio" name="optionsRadios"
                                        id="optionsRadios12" value="option2">Radio 2
                                    </label>
                                </div>
                                <div class="radio">
                                    <label> <input type="radio" name="optionsRadios"
                                        id="optionsRadios13" value="option3">Radio 3
                                    </label>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Inline Radio Buttons</label> <label class="radio-inline">
                                    <input type="radio" name="optionsRadiosInline"
                                    id="optionsRadiosInline1" value="option1" checked>1
                                </label> <label class="radio-inline"> <input type="radio"
                                    name="optionsRadiosInline" id="optionsRadiosInline2"
                                    value="option2">2
                                </label> <label class="radio-inline"> <input type="radio"
                                    name="optionsRadiosInline" id="optionsRadiosInline3"
                                    value="option3">3
                                </label>
                            </div>
                            <div class="form-group">
                                <label>Selects</label> <select class="form-control">
                                    <option>1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                    <option>5</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>Multiple Selects</label> <select multiple
                                    class="form-control">
                                    <option>1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                    <option>5</option>
                                </select>
                            </div>
                            <button type="submit" class="btn btn-default">Submit
                                Button</button>
                            <button type="reset" class="btn btn-default">Reset
                                Button</button>
                        </form>
                    </div>
                    <!-- /.col-lg-6 (nested) -->
                    <div class="col-lg-6">
                        <h1>Disabled Form States</h1>
                        <form role="form">
                            <fieldset disabled>
                                <div class="form-group">
                                    <label for="disabledSelect">Disabled input</label> <input
                                        class="form-control" id="disabledInput" type="text"
                                        placeholder="Disabled input" disabled>
                                </div>
                                <div class="form-group">
                                    <label for="disabledSelect">Disabled select menu</label> <select
                                        id="disabledSelect" class="form-control">
                                        <option>Disabled select</option>
                                    </select>
                                </div>
                                <div class="checkbox">
                                    <label> <input type="checkbox">Disabled
                                        Checkbox
                                    </label>
                                </div>
                                <button type="submit" class="btn btn-primary">Disabled
                                    Button</button>
                            </fieldset>
                        </form>
                        <h1>Form Validation States</h1>
                        <form role="form">
                            <div class="form-group has-success">
                                <label class="control-label" for="inputSuccess">Input
                                    with success</label> <input type="text" class="form-control"
                                    id="inputSuccess">
                            </div>
                            <div class="form-group has-warning">
                                <label class="control-label" for="inputWarning">Input
                                    with warning</label> <input type="text" class="form-control"
                                    id="inputWarning">
                            </div>
                            <div class="form-group has-error">
                                <label class="control-label" for="inputError">Input with
                                    error</label> <input type="text" class="form-control" id="inputError">
                            </div>
                        </form>
                        <h1>Input Groups</h1>
                        <form role="form">
                            <div class="form-group input-group">
                                <span class="input-group-addon">@</span> <input type="text"
                                    class="form-control" placeholder="Username">
                            </div>
                            <div class="form-group input-group">
                                <input type="text" class="form-control"> <span
                                    class="input-group-addon">.00</span>
                            </div>
                            <div class="form-group input-group">
                                <span class="input-group-addon"><i class="fa fa-eur"></i>
                                </span> <input type="text" class="form-control"
                                    placeholder="Font Awesome Icon">
                            </div>
                            <div class="form-group input-group">
                                <span class="input-group-addon">$</span> <input type="text"
                                    class="form-control"> <span class="input-group-addon">.00</span>
                            </div>
                            <div class="form-group input-group">
                                <input type="text" class="form-control"> <span
                                    class="input-group-btn">
                                    <button class="btn btn-default" type="button">
                                        <i class="fa fa-search"></i>
                                    </button>
                                </span>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MetalistEnterprises</groupId>
    <artifactId>MetalistEnterprises</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <spring.version>4.0.3.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>${spring.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</project>

的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MetalistEnterprises</display-name>
    <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>
</web-app>

第一控制器

@Controller
public class DefaultController {

    @Autowired
    IStockDetailsService iStockDetailsService;

    @RequestMapping(value = "/stock")
    public ModelAndView stockMethod(Model model) {
    ArrayList<StockBean> stocksList = iStockDetailsService.getAllStocks();
    model.addAttribute("stocksList", stocksList);
    return new ModelAndView("stock");
}

第二控制器

    @Controller
    public class StockController {

        @Autowired
        IStockDetailsService iStockDetailsService;

@RequestMapping(value = "/saveStock")
    public ModelAndView saveStock() {
        System.out.println("saveStock method called");
        iStockDetailsService.saveStock(stockBean);
        return new ModelAndView("redirect:/home");
    }

    }

public class StockBean {

    private Integer idStock;
    private String invoiceNumber;
    private Date invoiceDate;
    private String companyName;
    private String materialDescription;
    private float rate;
    private float amount;
    private float tax;

Getter and Setters
}

MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/jee 
         http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <context:component-scan base-package="metalist.yashdeep.scrap" />
    <context:annotation-config />
    <mvc:resources mapping="/resource/**" location="/resource/" />
    <mvc:annotation-driven />

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

0 个答案:

没有答案