Springmvc essay 1

1. Establish springmvc maven project.

To create a maven project in eclipse, select maven-archetype-webapp for Artifact Id. After the establishment, the package and tomcat runtime environment are missing, and then click java build path, then click jre to select the workspace’s own jre, at this time There will be maven java and resources packages. Then add server runtime and select tomcat. Exit after apply.

Add the maven warehouse address coordinates of spring-context, spring-core, spring-beans, spring-web, spring-webmvc, spring-aop, spring-expression, commons-logging (Apache)

p>

2. Add configuration in web.xml

Add SpringDispatchServlet, the servlet responsible for distribution. If the spring plug-in is installed in eclipse, it can be automatically generated here. After pressing alt+/, it is usually at the end of the prompt.

<servlet< span style="color: #0000ff;">>

<servlet-name>springDispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class< /span>>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value< span style="color: #0000ff;">>
init-param>
<load-on-startup>1load-on-startup< span style="color: #0000ff;">>
servlet>


<servlet-mapping>
<servlet-name>springDispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>

3. In the resouces directory, create a springxml file and configure two places, one is to configure the package for automatic scanning, and the other is to configure the view parser.

<context:component-scan < /span>base-package="cn.taotao.bean.handler">context:component-scan >

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver ">
<property name="prefix" value="/WEB-INF/views/">property >
<property name="suffix" value=".jsp">property< span style="color: #0000ff;">>
bean>

4. In the java directory, create a package, such as cn.taotao.bean.handler, then create a java file, mark the class with the annotation @Controller, and use @RequestMapping(“hellohref”) annotates the method, where the return value of the method is String type, and the returned string is the file name of the jsp to be displayed.

@Controller

public class HelloWorld {

@RequestMapping(
"hellohref")
public String SayHello() {

return "success";
}
}

5. In the web-inf directory, create the views directory (and the previous configuration Corresponding to each other), and then under views, create a success.jsp file.

6. Test, create a link in the index.jsp file on the homepage, such as

< div class="code">

<a href="hellohref">hello href linka>< /span>

After clicking, you can go to the success.jsp page under views.

<servlet>

<servlet-name>springDispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class< /span>>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value< span style="color: #0000ff;">>
init-param>
<load-on-startup>1load-on-startup< span style="color: #0000ff;">>
servlet>


<servlet-mapping>
<servlet-name>springDispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>

<context:component-scan base-package="cn.taotao.bean.handler ">context:component-scan>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver ">
<property name="prefix" value="/WEB-INF/views/">property >
<property name="suffix" value=".jsp">property< span style="color: #0000ff;">>
bean>

@Controller

public class HelloWorld {

@RequestMapping(
"hellohref")
public String SayHello() {

return "success";
}
}

<a href="hellohref">hello href linka< span style="color: #0000ff;">>

Leave a Comment

Your email address will not be published.