sping web mvc快速入门 三篇

SpringMVC_快速入门_组件解析 Spring与web环境集成 原始代码部分(通过tomcat启动访问) com.itheima.dao.UserDao public interface UserDao { public void save(); } com.itheima.dao.impl.UserDaoImpl public class UserDaoImpl implements UserDao { public void save() { System.out.println("save r 阅读详情

Basic Spring Web MVC Example

Posted in All by Jon on the February 10th, 2006

You want to get started with a simple web application that utilizes Spring’s web MVC framework. Here’s a simple project that provides such a start. It doesn’t use all of Spring’s features, nor even the best, but it will serve as a discussion point for future blog entries.

Background

When you’re putting together a simple web application, you need a few essentials:

  • A dispatcher servlet, which will intercept incoming requests.
  • A configuration that instructs the web container to route requests to the dispatcher servlet (ie. a servlet mapping)
  • Controllers (the C in MVC) that can perform the business logic and route to particular views (the V) perhaps conveying information models (the M).
  • A way for the dispatcher servlet to know which URLs to map to which controllers - called a url mapping
  • A way to determine, when a controller has done its job, how to get to the view - called a view resolver

The simple application here, skeleton1, doesn’t have a model at all, has a single controller (web.DispatchController), and a single view (myview.jsp)

How to do it and How it works

Download the source for the details. First, we want to use Spring’s DispatcherServlet as the entry point to our web application. You configure this simply in your web.xml:

<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/send/*</url-pattern>
</servlet-mapping>

Get it? This says: Create the servlet for me, call it dispatch, and map all incoming URLs that start with send to the dispatch servlet. So for example, a URL such as http://.../skeleton1/send/foobar would activate the dispatch servlet.

The rest of the configuration you do in a Spring web application context. Spring automatically looks for it using the name of the servlet as a hint. Since our servlet is called dispatch, it looks for a file called dispatch-servlet.xml in your WEB-INF directory. This configuration file will have three bits to it: the controller(s), the url mapping and the view resolver. First, the controller:

<bean id="dispatchController"
class="com.memestorm.web.DispatchController">
</bean>

No rocket science there. It’s simply a bean implementing the appropriate interface, which we’ll look at later.

The view resolver tells us how to render the output from a controller. Here’s the one I chose:

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

This uses a Spring-implemented InternalResourceViewResolver which basically maps to like-named views in the /WEB-INF/views directory, adding a .jsp. So for example, if our controller said “Please go to view myview,” then the above resolver would try to find the file myview.jsp in /WEB-INF/views. This is easy and extensible.

The final configuration is the URL mapping, which essentially tells the dispatcher servlet how to behave: Which incoming requests should it map to which controllers? We’ve chickened out:

<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/*">dispatchController</prop>
</props>
</property>
</bean>

This again uses a Spring-supplied URL mapper (isn’t it great, you don’t have to write any code) which, as you can see, maps all requests (/*) to the controller called dispatchController (which we defined above).

The last key to the puzzle is the controller itself. We know now that appropriate URLs (that are mapped to our dispatcher servlet) will be intercepted by the dispatcher servlet. It will then look at the URL, and on the basis of the URL mapper, map it to a particular controller (in this case, it maps all incoming URLs to the same controller), and to do this operationally it needs to invoke a method on the controller. Which method? Well, this particular URL mapper looks at the URL and “lops off” the start bit and uses what’s left. For example, if you went to http:.../skeleton1/send/actionName then it will try to invoke the method actionName() on dispatchController. Nothing too it. So, let’s check out the controller!

public class DispatchController extends MultiActionController {

public ModelAndView actionName(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("myview");
mav.addObject("message", "Hiya");
return mav;
}
}

Pretty simple. This class implements the Spring MultiActionController (which really means we can have multiple control methods defined here), and provides a single control action method called actionName(). Check out the parameters - pretty much like a servlet which isn’t too surprising. The result is: ModelAndView. This is really a wrapper that tells Spring what the model data is (what data do you want to send to your view) and which view to actually send it to. In this case, we want to go to the view myview and send a string along too.

If you’ve been paying attention, you’ll know that our view resolver will look for myview.jsp, and yes, that’s what we have. Here’s a look at it:

Hello, you're in myview.jsp! <br />
The controller sent me some data,
here it is: <%=request.getAttribute("message") %>

Nothing too it. It prints a message, and grabs the model from the appropriately named attribute in the request.

So, if we now invoked http://../skeleton1/send/actionName our action will get invoked, which will eventually forward to this JSP page. You can extend at will, adding additional action methods for example, additional controllers if you like, and of course, additional views.

Downloading

Download the source
skeleton1.zip
and read the installation instructions. Short version: Download Spring (with dependencies, I’m using Spring 2M2), modify one property in our build file to point to the unzipped Spring distribution, run the build with ant, deploy on a web container (I use Tomcat).

Advanced

You’ll note that the code does just a little more than described above. In particular, we create an additional root application context with two beans a,b, and we make sure that we access and call these beans from the controller. Oh, and we use the logging described in Refreshable log4j configuration files.

 


http://www.devx.com/Java/Article/22134

Simplify Your Web App Development Using the Spring MVC Framework
n a previous article, I introduced you to the Spring framework, showed you how to use Spring's basic functionality to create objects, and how to do some simple database interactions. In this follow-up article I will introduce you to the Spring model-view- controller (MVC) framework by walking you through the creation of a simple stock-trading Web application.


MVC is a pattern that helps separate presentation from business logic. In short, in an MVC application all Web requests are handled by controllers. A "controller" is responsible for interpreting the user's request and interacting with the application's business objects in order to fulfill the request. These business objects are represented as the "model" part of the MVC. Based on the outcome of the request execution, the controller decides which "view" to forward the model to. The view uses the data in the model to create the presentation that is returned to the user.

If you've ever worked on a Web application, chances are that you've worked with either a custom MVC framework or an existing framework, such as Struts. Struts is in fairly widespread use in the Java world, but the Spring MVC framework promises to provide a simpler alternative to Struts.

In this article I will walk you through the development of a Web-based stock-trading application. This application will have three primary workflows each of which will cover a different type of controller available in Spring. After reading this article, you should have enough technical and business knowledge to build a full-fledged stock-trading application and compete head-on with existing discount brokerage firms. … OK, not really, but you will at least have enough knowledge to get you started on your own Web projects using Spring.

You can download the Spring distribution from http://www.springframework.org/download.html. If you want to see the application in action, download the source code for this article, which includes a deployable war file.

Author's Note: The code in this article has only been tested on Tomcat 5.5.
The Platform
There are many different Web application servers and development environments to choose from. I won't describe any single environment here because I want to focus the discussion on the Spring framework and not the underlying tools. However, I developed this sample application using Tomcat 5.5, Java 1.5, Spring 1.1, and Eclipse 3.0 using the Sysdeo Tomcat plugin. I have used Spring with other application servers and with different versions of Java 1.4.x without any big surprises, so you should be safe if you have a slightly different environment.

Getting Started
Before diving into Spring, set up a mechanism for deploying your application code into your Web server and set up an application context if necessary. In Tomcat 5.5, the application context automatically takes on the name of the war file (or expanded war directory) that you deploy. My war file is called "tradingapp.war" and my context is consequently "/tradingapp."

Here is the basic directory structure that you need to create for your Web app:


/WEB-INF
/src (java classes will go in here)
/jsp (application jsps will go in here)
/lib (jar files that our app depends on will go in here)
/classes (compiled class files will go in here)
In order to test out the application server setup, create a file called index.jsp (as follows) and put it in the root of your application directory:

/index.jsp
<html>
<head><title>Trading App Test</title></head>
<body>
Trading App Test
</body>

Accessing a Controller
Like in other Web frameworks, Spring uses a servlet called DispatcherServlet to route all incoming requests (this pattern is sometimes called the Front Controller pattern). This servlet should be defined in the Web deployment descriptor as shown here:

/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
'http://java.sun.com/dtd/web-app_2_3.dtd'>

<web-app>
<servlet>
<servlet-name>tradingapp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>tradingapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
In the web.xml file I've defined a servlet mapping that forces any URL
that ends in .htm to reroute to the tradingapp Servlet (the
DispatcherServlet). This servlet analyzes the request URL and
determines which controller to pass control on to by using a URL
mapping defined in a Spring XML file. This Spring XML file must exist
in the /WEB-INF directory and it must have the same name as the servlet
name that you defined in the web.xml with a "-servlet.xml" appended to
it. Thus, we will create a file in the /WEB-INF directory called
The Portfolio Page
I first want to change the index.jsp page to redirect to my portfolio page by going through the PortfolioController. I'll also create a JSP called include.jsp that all of my JSP files can include in order to inherit a common set of properties and tag library definitions.

/index.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>

<core:redirect url="/portfolio.htm"/>


Now I'll create a simple portfolio view and have my controller route to it just to make sure everything is working.


/WEB-INF/jsp/portfolio.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>

<html>
<head><title>Portfolio Page</title></head>
<body>
Portfolio Page
</body>
</html>


Before writing the Controller, you should add a few jar files to your /WEB-INF/lib directory:
The Logon Page
Now you know how to create a page with dynamically updated content, but what good is a Web application that has no way to capture user input from an HTML form? We could implement the Controller interface in order to do form processing, but that would require us to do a lot of manual processing or request parameters and storing things on the session. Personally, I've hand coded more form-processing code in this fashion than I ever hope to again. Thus, out of sheer laziness, I will show you how Spring simplifies form handling via the SimpleFormController by building a logon page.
advertisement

Listing 4 shows the logon JSP. The <spring:bind> tag and the <spring:hasBindErrors> tag are in the spring.tld tag library descriptor that comes with Spring. Add this file to your WEB-INF directory and then add the following to your web.xml and include.jsp files.


/WEB-INF/web.xml
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>

/WEB-INF/jsp/include.jsp
<%@ page session="false"%>

<%@ taglib prefix="core" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="str" uri="http://jakarta.apache.org/taglibs/string-1.1" %>
<%@ taglib prefix="spring" uri="/spring" %>
The bind tag tells Spring to bind the enclosed form element to the bean property specified by the path attribute. For example the username input parameter will be bound to the username field on a bean named "credentials." The credentials bean is a special type of bean called a "command" or a "form-backing bean." Spring uses command beans for data binding while processing forms. The name used to reference this bean and its class type is defined in the tradingapp-servlet.xml file in the logonForm bean (see Listing 5).

I've also added a new entry to the handler mapping to point to our logon form when the user navigates (or is routed) to the logon.htm URL. The logon form has several properties:

  • commandClass—the class of the object that will be used to represent the data in this form.
  • commandName—the name of the command object.
  • sessionForm—if set to false, Spring uses a new bean instance (i.e. command object) per request, otherwise it will use the same bean instance for the duration of the session.
  • validator—a class that implements Spring's Validator interface, used to validate data that is passed in from the form.
  • formView—the JSP for the form, the user is sent here when the controller initially loads the form and when the form has been submitted with invalid data.
  • successView—the JSP that the user is routed to if the form submits with no validation errors.
Here is the code for the Controller:

/WEB-INF/src/com/devx/tradingapp/web/LogonFormController.java
package com.devx.tradingapp.web;

import javax.servlet.ServletException;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class LogonFormController extends SimpleFormController {
public ModelAndView onSubmit(Object command) throws ServletException {
return new ModelAndView(new RedirectView(getSuccessView()));
}

}
There's really not much to it. All the FormController does is forward to the success view. The validator takes care of authenticating the user. If the user provides an invalid username or password, the validator will return the user to the form and display an error message. Listing 6 shows the code for the validator.

The validator has a supports method and a validate method. The supports method is called to see if the validator supports a given object type. We want our validator to be able to validate objects of the type Credentials, because that is the command object that we are using for this form.

The validate method does some checks and calls the rejectValue method on the Errors class if validation fails. This method takes four parameters: the name of the field that failed validation, the key value of the error message that exists in a resource file (I'll explain in a second), a string array of values to substitute into the error message in the resource file, and the default error message that should be displayed if the resource file cannot be found. If any errors are found on the Errors object after validate is done running, Spring will forward control back to the original form JSP so that the errors can be displayed.

You may have noticed above that I also added a ResourceBundleMessageSource bean definition to the tradingapp-servlet.xml file. This is a reference to a properties file that contains application messages that can be defined and accessed throughout the application. These messages can be accessed in many ways, including being directly accessed from JSPs or, as we just saw, by the Errors object to provide error messages. This file must exist in the top level of your classpath, so I just put it in the WEB-INF/classes directory. I've jumped the gun and included all the error messages that our application will use in the file:


/WEB-INF/classes/messages.properties
error.login.not-specified=User credentials not specified (try guest/guest).
error.login.invalid-user=Username not valid, try 'guest'
error.login.invalid-pass=Password not valid, try 'guest'
error.trade.insufficient-funds=You do not have enough money to place this order
error.trade.not-enough-shares=You do not have that many shares
error.trade.dont-own=You don't own this stock
error.trade.invalid-symbol=Invalid ticker symbol: {0}
Here is the code for the infamous Credentials class (the command/form-backing object):

WEB-INF/src/com/devx/tradingapp/business/Credentials.java
package com.devx.tradingapp.business;

public class Credentials {
private String username;

private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;

}
}
Now you can change the index.jsp page in the root directory to point to logon.htm.

/index.jsp
<%@ include file="/WEB-INF/jsp/include.jsp" %>

<core:redirect url="/logon.htm"/>
Now if you load index.jsp, you will see the logon page, as shown in Figure 4.

Figure 4. Validating: When the logon page does not validate correctly, the user is returned to the form page where the validation errors are displayed.

In summary, here is the flow through this form:

  1. The user goes to the logon.htm page.
  2. He is routed to the dispatcher servlet (because all .htm pages are directed to the dispatcher Servlet).
  3. The DispatcherServlet loads the tradingapp-servlet.xml file and routes the user to the LogonFormController.
  4. The LogonFormController loads the command bean (form-backing object) and routes the user to the page defined in the formView (logon.jsp).
  5. The user fills out the form and submits it.
  6. The user is directed back to the controller and in turn the validator.
  7. If the validator fails, the user is sent back to the form view and error messages are displayed (back to logon.jsp and step 5).
  8. If the validator doesn't fail, the controller's onSubmit method is called and the user is forwarded to the successView JSP (portfolio.jsp).


Jar Description Source
spring.jar Main spring jar file [spring-dist]/dist
log4j-1.2.8.jar log4j logging package [spring-dist]/lib/log4j
commons-logging.jar Jakarta Commons logging package [spring-dist]/lib/jakarta-commons
jstl.jar Java standard tag library [spring-dist]/lib/j2ee
standard.jar Jakarta standard tag library [spring-dist]/lib/jakarta-taglibs
taglibs-string.jar Jakarta tag library used for String manipulation http://jakarta.apache.org/taglibs/
commons-lang-2.0.jar Jakarta Commons language API http://jakarta.apache.org/commons
jfl.jar Financial library used to get stock-quotes online http://www.neuro-tech.net/archives/000032.html

Most of these jars are available in the Spring distribution. All of them are available with the downloadable code for this article.

Now create a class called PortfolioController:

/WEB-INF/src/com/devx/tradingapp/web/PortfolioController.java package com.devx.tradingapp.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class PortfolioController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { return new ModelAndView("/WEB-INF/jsp/portfolio.jsp"); } } The Controller interface defines a single method signature: public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception;
Figure 2. Back to the Portfolio: The index.jsp page should now redirect you to the Portfolio Page.

The object that is returned by the handleRequest method is of the type ModelAndView. As you probably guessed, the ModelAndView object represents the Model and View in the MVC pattern. ModelAndView has several contructors. The one we're using right now just takes a string that represents the view that we want to forward to. Because our portfolio.jsp page doesn't have any dynamic content (yet), we don't need to return a model object.

Compile the PortfolioController and make sure the compiled file is under the WEB-INF/classes directory structure (i.e. /WEB-INF/classes/com/devx/tradingapp/web/PortfolioController.class). Now you can deploy your application and pull up the index.jsp page again. Go to http://localhost:8080/tradingapp/index.jsp and you should see what is shown in Figure 2.

Now I'll make it a little easier to specify views from within the Controller objects. The goal is to avoid having to type out the full path to a JSP inside of my Controller code. I can achieve this through use of a ViewResolver that I'll define in my tradingapp-servlet.xml file. The ViewResolver appends a prefix and suffix to the view that the Controller returns.

/WEB-INF/tradingapp-servlet.xml <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property> <property name="prefix"><value>/WEB-INF/jsp/</value></property> <property name="suffix"><value>.jsp</value></property> </bean> Now I can simplify my PortfolioController: /WEB-INF/src/com/devx/tradingapp/web/PortfolioController.java package com.devx.tradingapp.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class PortfolioController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { return new ModelAndView("portfolio"); } } Just to make sure that the Portfolio Page still loads after this change, reload the page in your Web browser (you may have to reload the application context or restart your application server if your application server does not support hot-deploy functionality).

Now I want to make the portfolio page more interesting! First, I'll add some custom-tag library definitions to the include.jsp file. Using custom tags helps me keep my presentation logic separate from the presentation itself.

/WEB-INF/jsp/include.jsp <%@ page session="false"%> <%@ taglib prefix="core" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> <%@ taglib prefix="str" uri="http://jakarta.apache.org/taglibs/string-1.1" %> Next, update your PortfolioController (see the code in Listing 1).

In the new version of the Controller, I've added a model. I use the three-argument constructor for ModelAndView that takes the view, the string name that the JSPs will use to refer to the model, and the model object itself. In Spring, a model is usually just a java.util.Map object. I put two elements on the model, a cash amount, and a list of portfolio line items. Each line item is of type PortfolioItemBean, a JavaBean that I created. This bean is primarily for View purposes and is created using data from the underlying business object called Portfolio. Listing 2 shows these classes.

You'll also notice that I'm using a class called QuoteFactory to obtain a Quote object using a stock symbol. These classes are part of the Neurotech Java Financial Library, a simple, open source API that can be used to retrieve stock quotes from the Web as well as other simple financial tasks.

Listing 3 shows the updated tradingapp-servlet.xml file and the portfolio.jsp file. If all goes well, when you deploy and reload the page you should see something very similar to Figure 3.

Figure 3. Improving the Portfolio: The portfolio view uses custom tags to display model data that is provided by the PortfolioController.

The cool thing is that the PortfolioController retrieves quotes from the Web using the Java Financial Library, so if you keep refreshing the page during trading hours, you'll get updated quotes.

In summary, here is the flow to this page:

  1. The user goes to the portfolio.htm page.
  2. He is routed to the dispatcher Servlet (because all .htm pages are directed to the dispatcher Servlet).
  3. The DispatcherServlet loads the tradingapp-servlet.xml file and routes the user to the PortfolioController.
  4. The PortfolioController creates the model and view and passes control back to the DispatcherServlet.
  5. The DispatcherServlet makes the model data accessible via session or request parameters.
  6. The DispatcherServlet routes to the JSP pages.
  7. The JSP is rendered and the presentation is sent back to the user.


/WEB-INF/jsp/include.jsp
<%@ page session="false"%>

"tradingapp-servlet.xml."

Here is the Spring XML file that the DispatcherServlet will use:

/WEB-INF/tradingapp-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="portfolioController" class="com.devx.tradingapp.web.portfolio.PortfolioController"> </bean> <!-- you can have more than one handler defined --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/portfolio.htm"> <ref bean="portfolioController" /> </entry> </map> </property> </bean> </beans> I called the bean urlMapping, but Spring should pick up the mapping regardless of what you name it. In fact, you can have multiple handler-mapping objects defined. The SimpleUrlHandlerMapping class has a map property called urlMap that maps URLs to objects that implement the Controller interface. When the DispatcherServlet looks in this bean definition file, it will load the mapping and use it to determine which controller to route to. I've created a reference to the PortfolioController.

There are several different types of controllers available in the Spring framework. In this article, I will show you three different types:

  1. For the portfolio page, I will create the simplest type of controller: one that implements the Controller interface directly.
  2. Next I'll create a logon form that will use a SimpleFormController class.
  3. Last, I'll create a trade wizard using the AbstractWizardFormController class.

</web-app>

</html>
Figure 1. Clear the Deck: Pull up the index.jsp page to make sure that you can retrieve a simple JSP page.

Try to pull up the page by going to http://localhost:8080/tradingapp/index.jsp (see Figure 1). (The port may vary depending on the application server you are using.)

On most application servers, users can access files in the root directory of the context. Hence, you were able to hit the index.jsp file directly. In an MVC architecture, you want the controller to handle all incoming requests. In order to do this, it is a good idea to keep your JSP files in a place where they are not directly accessible to the user. That is why we will put all of our application JSP files in the WEB-INF/jsp directory. We'll come back to this soon, but for now let's find out how to access a Spring controller.

Page 1 of 5

The Trade Wizard
Often times we have more than one screen that a user interacts with in order to complete a given task. This sequence of screens is often called a "wizard." The last MVC component I will introduce you to is the AbstractWizardFormController. This controller allows you to carry the same command object through an entire flow of Web pages, thus allowing you to break up the presentation into multiple stages that act on the same model.

For the example, I'll create a wizard that allows a user to trade stock. This wizard will start with a page that takes the order and then goes to a confirmation page where the user will choose to either execute or cancel the order. If the order is cancelled, the user will be returned to the portfolio page, if the order is executed the user is sent to an acknowledgement page to tell them that their order was filled.

First I need a way to get to the trade page, so I'll put a link to the trade page on the portfolio page.


/WEB-INF/jsp/portfolio.jsp
<br>
<a href="<core:url value="trade.htm"/>">Make a trade</a><br/>
<a href="<core:url value="logon.htm"/>">Log out</a>
<br>
</body>
</html>
Now I'll update the tradingapp-servlet.xml so that it knows what to do when the user clicks on the trade.htm link.

/WEB-INF/tradingapp-servlet.xml
<bean id="tradeForm" class="com.devx.tradingapp.web. TradeFormController">
<constructor-arg index="0">
<ref bean="portfolio"/>
</constructor-arg>
</bean>

<!-- you can have more than one handler defined -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/portfolio.htm">
<ref bean="portfolioController" />
</entry>
<entry key="/logon.htm">
<ref bean="logonForm"/>
</entry>
<entry key="/trade.htm">
<ref bean="tradeForm"/>
</entry>
</map>
</property>
</bean>
The TradeFormController should exist as /WEB-INF/src/com/devx/tradingapp/web/TradeFormController.java. But there is a lot to the TradeFormController, so I'll explain it in segments.


public class TradeFormController extends AbstractWizardFormController {

private Portfolio portfolio;

public TradeFormController(Portfolio portfolio) {
this.portfolio = portfolio;
setPages(new String[] { "trade", "trade-confirm" });
setCommandName("trade");
}
The TradeFormController extends AbstractWizardFormController. I pass in a Portfolio object so that the trade form knows what my buying limit is and can add and remove stock from the portfolio. The setPages method in the constructor tells the form the sequence of pages we are going to call.

The strings in this array relate to views that are resolved by the ViewResolver just like in the other controllers. These pages are indexed by Spring starting at 0. Thus the trade view is index 0 and the trade-confirm view is index 1. The indexes exist because we may have an event that causes us to skip or go back to a previous page. The setCommandName method sets the name of the command object that we are going to use for this form. This object will be created in the formBackingObject method.


protected Object formBackingObject(HttpServletRequest request) {
Trade trade = new Trade();
trade.setBuySell(Trade.BUY);
return trade;
}
This method will create the command object and set it in its initial state. This method is called before the user is directed to the first page of the wizard. Subsequent submits will call several methods on the abstract controller. The main methods that are called are onBind, validatePage, and getTargetPage.

onBind
protected void onBind(HttpServletRequest request, Object command,
BindException errors) {

Trade trade = (Trade) command;

if (symbolIsInvalid(trade.getSymbol())) {
errors.rejectValue("symbol", "error.trade.invalid-symbol",
new Object[] { trade.getSymbol() },
"Invalid ticker symbol.");
} else {
Quote quote = null;
try {
quote = new QuoteFactory().getQuote(trade.getSymbol());
} catch (QuoteException e) {
throw new RuntimeException(e);
}
trade.setPrice(quote.getValue());
trade.setSymbol(trade.getSymbol().toUpperCase());
}
}
The onBind method is called before any validation occurs for each submit. I override the onBind method in order to get a quote on the symbol the user is trying to purchase and set the price and symbol on the command object. Keep in mind that even if onBind adds errors to the BindException, the validatePage method will still be called.

protected void validatePage(Object command, Errors errors, int page) {
Trade trade = (Trade) command;

if (tradeIsBuy(trade)) {
if (insufficientFunds(trade)) {
errors.reject("error.trade.insufficient-funds",
"Insufficient funds.");
}
} else if (tradeIsSell(trade)) {
if (portfolio.contains(trade.getSymbol()) == false) {
errors.rejectValue("symbol", "error.trade.dont-own",
"You don't own this stock.");
} else if (notEnoughShares(trade)) {
errors.rejectValue("quantity", "error.trade.not-enough-shares",
"Not enough shares.");
}
}
}
validatePage
The validatePage method is called after the onBind method. It acts in the same way as the validator did in the logon controller. The validatePage method is passed the page number for the page in the flow that you are validating. This can be used if you need to do custom validation for each page in the flow. If you wanted to, you could create validator objects for each page and use the validatePage method to delegate to the appropriate validator based on the page index that was passed in.

getTargetPage
The getTargetPage method will be called in order to find out which page to navigate to. This method can be overridden in your controller, but I use the default implementation, which looks for a request parameter starting with "_target" and ending with a number (e.g. "_target1"). The JSP pages should provide these request parameters so that the wizard knows which page to go to even when the user uses the Web-browser's back button.

The processFinish method is called if there is a submit that validates and contains the "_finish" request parameter. The processCancel method is called if there is a submit that contains the "_cancel" request parameter.


protected ModelAndView processFinish(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) {
Trade trade = (Trade) command;

if (trade.isBuySell() == Trade.BUY) {
portfolio.buyStock(trade.getSymbol(), trade.getShares(), trade
.getPrice());
} else {
portfolio.sellStock(trade.getSymbol(), trade.getShares(), trade
.getPrice());
}
return new ModelAndView("trade-acknowledge", "trade", trade);
}

protected ModelAndView processCancel(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) {
return new ModelAndView(new RedirectView("portfolio.htm"));



 
	


}
Listing 7 is the code for the Trade class (our command for this form). And now all you need are the JSP pages (see Listing 8 for the three JSP pages).

As you can see in the code, the Spring-specific request parameters are specified in the submit button name attribute. If all goes well, you should be able to pull up the first page of the trade wizard by going to http://localhost:8080/tradingapp/trade.htm (see Figure 5).

Figure 5. Ready to Trade: The first page of the trade wizard allows you place an order for a stock trade.

In summary, here is the flow through this form:

  1. The user goes to the trade.htm page.
  2. He is routed to the dispatcher servlet (because all .htm pages are directed to the dispatcher servlet).
  3. The DispatcherServlet loads the tradingapp-servlet.xml file and routes the user to the TradeFormController.
  4. The TradeFormController loads the command bean by calling the formBackingObject method and routes the user to the first page defined in the setPages call in the constructor (trade.jsp).
  5. The user fills out the form and submits it.
  6. The user is directed back to the controller, which parses the target page off of the request parameters from trade.jsp, binds and validates the command object, and forwards the user to the next page in the wizard.
  7. If the validator fails, the user is sent back to the form view and error messages are displayed (back to trade.jsp).
  8. If the validator doesn't fail, the controller forwards to the trade-confirm.jsp page.
  9. The user submits an execute or cancel command, which will in turn tell the controller to execute either the processFinish or processCancel commands, respectively.
  10. The user is forwarded to the page that the processFinish or processCancel command sends them to.

I've gone over three different types of controllers provided by Spring, which should be enough to get you started on your own Web projects. The Spring framework contains quite a few more features than those I have gone over in this article and its predecessor. I have found Spring to be a very valuable tool in my software development toolbox, and I urge you to discover its vast utility for yourself.


 

Javid Jamae consults for Valtech, a global consulting group
specializing in delivering advanced technology solutions. Valtech
endeavors to help its
customers through its global delivery model to create and/or maintain
an affordable competitive advantage.


 

http://blog.youkuaiyun.com/paddyhuang/archive/2004/08/17/76488.aspx

Handler Mapping在Spring的MVC中

说到Mapping问题。。我就想到了Controller Front模式(请见:http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html)

所以说呢,Mapping问题主要就是要解决要调用哪一个Action的问题。。

主要有两个Mapping Handler方式:

1. BeanNameUrlHandlerMapping

这个其实也就是默认方式,只要你不要**-servlet.xml中定义其mapping方式的话,Spring会自动选择其方式

它是根据其所定义的Action的ID进行调用:

比如:

<beans>
  <bean id=”mapping” class=”org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping”/>
  <!-- 定义几个Action bean -->
  <bean id=”/addUser.html” class=”org.springframework.web.mvc.SimpleFormController”>
    <property .................../>设置其SimpleFormControl的一些属性
 </bean>
<bean id=”/logout.html” class=”saoo.MyController”/>
</beans>

所以,它所对应要增加一个新的User的URL就是/addUser.html
而一但用户想登出系统的话,就可以通过URL:/logout.html来实现

 

2. SimpleUrlHandlerMapping

它是通过原先配置好的一对一对放入到其一个Map mappings的属性里面
比如:
<beans>
  <bean id=”mapping” class=”org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping”/>
  <!-- 定义几个Action bean -->
  <bean id=”/addUser.html” class=”org.springframework.web.mvc.SimpleFormController”>
    <property .................../>设置其SimpleFormControl的一些属性
 </bean>
<bean id=”/logout.html” class=”saoo.MyController”/>
</beans>
可以改成:
<beans>
  <bean id=”mapping” class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>
    <props><!--因为其属性是Map-->
      <prop key="/addUser.html">AddUserController</prop><!--因对应的是Action Bean的id-->
      <prop key="/logout.html">Logout</prop><!--对应Logout的Action-->
    </props>
  </bean>
  <!-- 定义几个Action bean -->
  <bean id=”AddUserController” class=”org.springframework.web.mvc.SimpleFormController”>
    <property .................../>设置其SimpleFormControl的一些属性
 </bean>
<bean id=”Logout” class=”saoo.MyController”/>
</beans>

过时候再与一些关于Action的东东。。呵呵


 

注意:mapping controller的时候,有个路径的问题。

如果

  <servlet-mapping>
    <servlet-name>processconfig</servlet-name>
    <url-pattern>/pc/agr/*</url-pattern>
</servlet-mapping>

那么

<bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/app">tGuiderConf</prop>
        </props>
    </property>
    </bean>

将把

http://../pc/agr/app映射到tGuiderConf这个controller。

而不是

http://../app。

快速掌握Spring MVC开发指南 MVC(Model-View-Controller)是一种设计模式,旨在将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。模型负责数据和业务逻辑,视图负责展示数据,而控制器处理用户的输入,并将模型和视图关联起来。在Spring MVC框架中,模型(Model)是应用程序中用来封装业务对象以及传递数据给视图(View)的部分。模型的主要职责是与业务逻辑层进行交互,获取数据,并将数据以一种形式呈现给视图层进行展示。 阅读详情

相关推荐

Windows Server 2019整机备份与异机还原实战:从虚拟机到物理机的完整迁移指南

本文详细介绍了Windows Server 2019整机备份与异机还原的实战操作,涵盖从虚拟机到物理机的完整迁移流程。通过环境准备、备份策略设计、具体操作步骤及迁移后优化等内容,帮助用户实现系统环境的无缝迁移,特别适用于服务器硬件升级、数据中心迁移等场景。

weixin_30555125的博客 133

Cannot render error page for request [/xxx/xxx/xxx] and exception [nested exception is org.apache.

Cannot render error page for request [/xxx/xxx/xxx] and exception [nested exception is org.apache.ibatis.builder.BuilderException,纯纯语法错误!!!!

Chao_nengli的博客 7976

Unity适配Nintendo Switch平台开发

查看文档、下载SDK,都需要账号登录。

凝遥的笔记本 2102

关于Cannot forward to error page for request [/strategy/list/] 。。。的问题

 今天遇到这个一个错误:Cannot forward to error page for request [/wechat/wechatCheck/forgetPsd] as the response has already been committed. As a result, the response may have the wrong status code. If your appli...

qq_33551131的博客 3万+

Unsloth Desktop 本地大模型微调量化实战指南

大语言模型的本地化定制正在成为开发者与内容创作者的重要需求,而微调与量化则是其中最关键的两项技术。LoRA 与 QLoRA 通过仅训练少量低秩矩阵,大幅降低显存占用和训练成本,让消费级 GPU 也能完成模型适配;GGUF 格式则打通了训练与推理的边界,使微调后的模型可直接接入 Ollama、llama.cpp 等主流推理引擎。本文以 Unsloth Desktop 为切入对象,从其底层加速原理出发,梳理了环境准备、模型加载、对话测试、量化导出、微调验证及 API 批量调用的完整链路,并针对显存优化、端口冲突

weixin_29055137的博客 399

Calibre LVS错误排查实战:从Innovus到GDS的完整调试流程(附常见错误解析)

本文详细介绍了数字IC后端设计中,从Innovus工具输出数据到Calibre LVS物理验证的完整调试流程。文章系统性地讲解了LVS错误排查的方法论,包括报告解读、图形化定位以及针对器件不匹配、电源网络错误、短路开路等常见问题的深度解析与解决方案,旨在帮助工程师高效完成版图与原理图的一致性检查。

weixin_29218963的博客 776

整合篇:零基础学习与使用WebMVC

目录1、WEB开发1.1、静态资源处理1.2、欢迎页的支持1.3、错误页的支持1.4、网站图标支持1.5、RESTful 风格1.6、原生组件注册1.6.1、注解形式1.6.2、注册形式1.7、支持异步请求1.8、支持定时任务2、日志记录2.1、创建工程2.2、日志使用2.3、日志设置 配套资料,免费下载 1、WEB开发 1.1、静态资源处理 静态资源目录 只要静态资源放在类路径下中的:/static、/public、/resources、/META-INF/resources目录中,均可以被访问。 访问

轻松的小希 2700

S7-200 Smart 控制运动轴(脉冲电机/伺服)教程

S7-200 Smart 控制运动轴(脉冲电机/伺服) 结合本人刚接触触摸屏时候的踩坑经验,编写S7-200 Smart 控制运动轴(脉冲电机/伺服)的详细流程,给刚接触PLC的朋友提供一些经验指导。 本文详细介绍了S7-200 Smart 控制脉冲步进/伺服电机流程,包含接线方法及梯形图。希望对初学者有所帮助

xjbsw0的博客 5万+

Sping Web MVC入门

xkroy的博客 298

电脑硬件:电脑主板上的接口介绍,看完你就懂了

它们主要用于连接老式的键盘和鼠标,虽然速度和兼容性不如USB接口,但在某些特定场合依然发挥着重要作用。PCIe接口是主板上的扩展能力象征,可以连接各种扩展卡,如显卡、声卡、网卡等。电脑主板作为电脑硬件组成关键的部分,今天给大家分享一下电脑主板上有哪些接口,具体每一个接口的特点、使用场景,有需要的朋友可以来了解一下!它们不仅能连接鼠标、键盘、U盘、打印机,还能连接各种外设,如移动硬盘、游戏手柄等。:虽然现在用得越来越少,但在一些特定场合,如工业控制、老旧设备上,仍然需要使用PS/2接口。

IT技术分享社区 2788

Azure AI-102 认证全攻略: (二十八) 设置 Azure AI 服务 API

在本文中,您学习了如何通过 Azure 门户设置和管理 Azure AI 服务,以及如何通过编程方式访问这些服务。在这篇文章中,我们将深入探讨如何设置 Azure AI 服务,特别是如何通过 Azure API 来进行编程访问,以实现自动化和集成。通过本篇文章的学习,您不仅能够顺利完成 Azure AI-102 认证中的 API 配置和集成内容,还能高效地管理和优化 Azure AI 服务,构建稳定、可靠的 AI 应用。在成功设置并测试了 API 后,接下来要管理和维护您的 Azure AI 服务。

高校图书馆高级系统架构师/研究馆员,推动高校AI从概念演示到可评审、可交付、可运维:路线图与立项评审、PoC交付、评测回归与上线运维(Baseline/Eval/Ops)。 1447

什么是Spring与SpringBoot?什么是MVC与SpringMVC?

Spring本身是一个非常强大的企业级应用框架,它可以提高项目开发效率,降低可侵入性,将“高内聚,低耦合”的软件设计思路发挥到了极致。但也有很多缺点,如下:1.Spring框架需要依赖很多jar包,程序开发人员每次搭建 Spring框架都要下载十多个jar 包。由于 Spring 版本频繁地更新,导致不同版本的 Spring 对于依赖的jar 包有着严格的版本要求。即使程序开发人员拥有早期版本的jar 包,也会因为与 Spring 更新后的版本不兼容而导致项目无法运行。

2301_79575831的博客 1382

从Raw图像到超分辨率:真实场景下的图像增强全流程指南

通过本文的学习,您已掌握了利用Raw图像实现真实场景超分辨率的完整技术链——从数据生成的“真实感模拟”,到双分支CNN的“细节+色彩”协同建模,再到模型的训练与落地。本文将带您从理论到实践,掌握利用Raw图像实现真实场景超分辨率的核心技术,让您的图像在细节和色彩上都能“更上一层楼”。是相机传感器直接输出的原始数据,保留了最完整的亮度、色彩信息(如Bayer阵列的原始采样),这为超分辨率提供了“富矿级”的信息基础。该分支以低分辨率RGB图像为参考,学习“色彩变换矩阵”,让恢复的细节拥有真实的色彩表现。

FJN110的博客 144

Sping、SpringMVC、SpringBoot的对比

原文链接:https://dzone.com/articles/spring-boot-vs-spring-mvc-vs-spring-how-do-they-compare 作者: Ranga Karanam 译者:Yunooa 推荐关注公众号: 锅外的大佬 每日推送,专注分享国外最新技术内容,帮助每位开发者更优秀地成长 在本文中,你将获取到有关于Spring,Spring MVC和Sprin...

liululee的博客 395

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Sping6知识点概述(带你快速入门和回顾)

spring6学习笔记!

weixin_44729515的博客 1281

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Spring MVC快速入门

今天给大家介绍一下Spring MVC,让我们学习一下如何利用Spring MVC快速的搭建一个简单的web应用。 更深入地学习Spring MVC,请大家参考Spring MVC实战入门训练。 参考代码请戳右上角,下载下来后可以在Eclipse或者IntelliJ中导入为一个Maven项目。 环境准备 一个称手的文本编辑器(例如Vim、Emacs、Sublime Text

klayer_cong的博客 1452

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Spring MVC 与 Spring Boot

Spring MVC 是 "工具":解决 Web 层的请求处理问题,是 Spring 生态的 Web 核心。Spring Boot 是 "加速器":基于 Spring MVC 等 Spring 组件,简化开发流程,让开发者更专注于业务逻辑而非配置。Spring Boot = Spring MVC + 自动配置 + 起步依赖 + 嵌入式服务器 + ...,它是使用 Spring MVC 的最佳实践方式。

2502_92141759的博客 759

Springboot1学习(一)快速入门

拖了好久,终于学开始springboot,馒馒来吧。 看了看还有好多没学完… 先学springboot1吧,磨刀不误砍柴功 文章目录springboot概述快速入门第二种启动方式静态资源访问全局异常捕获FreeMarker页面渲染 springboot概述 支离破碎的语言随便写一下吧,反正都不看的。捂脸 目前spingboot主流 敏捷开发 无需tomcat,内置了tomcat 减少xml配置,改用properties springcloud+springboot微服务技术 springboot底层封

qq_19841133的博客 452

sping MVC框架

上次介绍了一下ssh框架,  这次就简单的介绍一下MVC框架。 Spring MVC 介绍 Spring的一部分 Spring springmvc 无缝衔接 SpringMVC 是一个web层框架。  用来接收参数,和响应请求 快速搭建: 需是web项目,还要web.xml去配置一个springmvc核心接收参数的类,springMVC用的是servlet开发的      1. 创建...

weixin_42108509的博客 236

sping boot基础

通过本文的介绍,你对 Spring Boot 的基础概念和基本使用方法有了一个初步的了解。Spring Boot 以其简洁高效的开发方式,在 Java 企业级应用开发中得到了广泛的应用。它不仅大大简化了项目的初始搭建过程,还通过自动配置等特性提高了开发效率,让开发者能够更加专注于业务逻辑的实现。在后续的学习中,你可以进一步深入了解 Spring Boot 的更多高级特性,如数据库访问、安全管理、缓存机制等,以构建更加完善和强大的应用系统。

2401_84461036的博客 506

【spring】Spring、Spring MVC、Spring Boot、Spring Cloud?

Spring、Spring MVC、Spring Boot、Spring Cloud?

weixin_46589153的博客 650

Spring MVC快速体验

Spring MVC 快速体验的五个步骤: 1. 配置web.xml 2. 创建Spring配置文件applicationContext.xml 3. 创建model 4. 创建controller 5. 创建视图文件userlist.jsp 6. 创建Servlet配置文件Spring-servlet.xml

栋先生 612

Idea 七步建立Spring Mvc 的web项目,极其简单

Idea 7步建立Spring Mvc 的web项目 1.新建项目 新建的结构是这样的 2.添加controller的包、文件;添加jsp的文件夹和jsp文件 文件名为HelloWorldController ,代码在下方 文件名为hello.jsp,代码在下方 完整的目录结构如下: 代码 package test.con...

weixin_30483697的博客 216
上一篇: 软件项目版本号的命名格式
下一篇: 关于java和web项目中的相对路径问题
yethyeth
博客等级 码龄21年 140粉丝 265原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值