zoukankan      html  css  js  c++  java
  • spring:设置映射访问路径 或 xml配置访问路径 (spring mvc form表单)

    项目hello,

    在src/main/java下面建一个目录: charpter2

    一.xml配置访问路径

    web.xml

    <web-app>
    
      <display-name>Archetype Created Web Application</display-name>
      
    
    
      <servlet>  
        <servlet-name>chapter2</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>chapter2</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>   
    
    </web-app>
    

    chapter2-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					       http://www.springframework.org/schema/beans/spring-beans.xsd 
    					       http://www.springframework.org/schema/mvc 
    					       http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    					       http://www.springframework.org/schema/context 
    					       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- HandlerAdapter --> 
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        
    
    <!-- ViewResolver -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    	<property name="prefix" value="/WEB-INF/jsp/"/>
    	<property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 处理器 -->  
    <bean name="/helloworld" class="chapter2.web.controller.HelloWorldController"/>  
    <!--
    <bean name="/student" class="chapter2.web.controller.StudentController"/>  
    -->
           
    </beans>    
    

      

    chapter2/HelloWorldController.java

    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 HelloWorldController implements Controller {
    
    	public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
    		// TODO Auto-generated method stub
    			ModelAndView mv = new ModelAndView();  
    	       //添加模型数据 可以是任意的POJO对象  
    	       mv.addObject("message", "Hello World!");  
    	       //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面  
    	       mv.setViewName("hello");  
    	       return mv;  
    	}
    
    }
    

      

    注意在 chapter2-servlet.xml中添加处理器配置:

    <!-- 处理器 -->  
    <bean name="/helloworld" class="chapter2.web.controller.HelloWorldController"/>  

    然后再WEB-INF/目录下新建jsp目录
    web-inf/jsp/hello.jsp
    <%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ page isELIgnored="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>hello</title>
    </head>
    <body>
    
    dddd
    <br>
    ${message}
    
    
    </body>
    </html>
    

      访问地址是: 项目地址/hello/helloworld

    二。映射访问配置

    首先配置文件有3: applicationContext.xml, web.xml, chapter2-servlet.xml

    web.xml

    <!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>
    
      <display-name>Archetype Created Web Application</display-name>
      
    <!--配置文件路径-->
    <context-param>
     	<param-name>contextConfigLocation</param-name>
    	<param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
      <servlet>  
        <servlet-name>chapter2</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>chapter2</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>   
    
    </web-app>
    

      chapter2-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					       http://www.springframework.org/schema/beans/spring-beans.xsd 
    					       http://www.springframework.org/schema/mvc 
    					       http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    					       http://www.springframework.org/schema/context 
    					       http://www.springframework.org/schema/context/spring-context.xsd">
          
     
    
    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven /> 
     <!-- 自动扫描的包名 -->
    <context:component-scan base-package="chapter2.*" />
    
          
           
    </beans>       
    

      applicationContext.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					       http://www.springframework.org/schema/beans/spring-beans.xsd 
    					       http://www.springframework.org/schema/mvc 
    					       http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    					       http://www.springframework.org/schema/context 
    					       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- HandlerAdapter --> 
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        
    
    <!-- ViewResolver -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    	<property name="prefix" value="/WEB-INF/jsp/"/>
    	<property name="suffix" value=".jsp"/>
    </bean>
    
    
           
    </beans>   
    

      

    r然后就是java代码:

    chapter2/Student.java

    public class Student {
    
    	
    	private Integer id;
    	private Integer age;
    	private String name;
    	
    	public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	
    }
    

      chapter2/StudentController.java

    import org.springframework.stereotype.Controller;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.ui.ModelMap;
    
    
    @Controller
    public class StudentController {
    
    	
    	@RequestMapping(value="/student", method=RequestMethod.GET)
    	public ModelAndView student()
    	{
    		return new ModelAndView("student", "command", new Student());
    		
    	}
    	
    	@RequestMapping(value="/assStudent", method=RequestMethod.POST)
    	public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model)
    	{
    		model.addAttribute("name", student.getName());
    		model.addAttribute("age", student.getAge());
    		model.addAttribute("id", student.getId());
    		
    		return "student_result";
    		
    	}
    	
    	
    	
    }
    

     

    jsp文件

    student.js

    <%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Spring MVC表单处理</title>
    </head>
    <body>
    
    
    <h2>Student Information</h2>
    <form:form method="post" action="/hello/addStudent">
    <table>
    	<tr>
    		<td><form:label path="name">姓名:</form:label></td>
    		<td><form:input path="name"/></td>
    	</tr>
    	<tr>
    		<td><form:label path="age">年龄</form:label></td>
    		<td><form:input path="age"/></td>
    	</tr>
    	<tr>
    		<td><form:label path="id">编号</form:label></td>
    		<td><form:input path="id"/></td>
    	</tr>
    	<tr>
    		<td colspan="2">
    			<input type="submit" value="提交表单"/>
    		</td>
    	</tr>
    </table>
    </form:form>
    
    </body>
    </html>
    

      student_result.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ page isELIgnored="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>spring mvc表单处理</title>
    </head>
    <body>
    
    
    <h2>提交的学生信息</h2>
    <table>
    	<tr>
    		<td>名称:</td>
    		<td>${name}</td>
    	</tr>
    	<tr>
    		<td>年龄:</td>
    		<td>${age}</td>
    	</tr>
    	<tr>
    		<td>编号:</td>
    		<td>${id}</td>
    	</tr>
    </table>
    
    </body>
    </html>
    

      

     访问地址是: 项目地址/hello/student

           访问地址:项目地址/hello/addStudent



  • 相关阅读:
    django 项目需要注意的一些点
    VUE之路
    Oracle 表格碎片的查看方法
    RHEL 6.x or 7.x 使用分区绑定ASM 磁盘的方法
    RMAN 修复主库 nologging 操作导致物理备库的坏块
    Oracle 数据库19c 回退降级到 11.2.0.4 方案
    如何评估oracle 数据库rman全备和增量备份大小
    在将Oracle GI和DB升级到19c或降级到以前的版本之前需要应用的补丁 (Doc ID 2668071.1)
    Oracle 数据库坏块处理
    opatch auto 安装11.2.0.4.20190115 PSU遇到 OUI-67133: Execution of PRE script failed,with returen value 1 报错
  • 原文地址:https://www.cnblogs.com/achengmu/p/8884349.html
Copyright © 2011-2022 走看看