无法从控制器获取值,以便在春天从pojos

时间:2016-08-11 07:39:30

标签: spring jsp spring-mvc

我有以下代码

控制器

@Controller
public class ProductController {

     @RequestMapping("/products")
    public String list(Model model) {
        Product iphone = new Product("P1233", "iPhone 5s", new BigDecimal(500));
        iphone.setDescription("pple iPhone 5s smartphone with 4.00-inch 640x1136 display and 8-megapixel rear camera");
        iphone.setCategory("smart phone");
        iphone.setManfactuer("Apple");
        iphone.setUnitsInStock(1000);
        model.addAttribute("product", iphone);
        return "products";

    }

产品

private String productId;
    private String name;
    private BigDecimal unitPrice;
    private String description;
    private String manfactuer;
    private String category;
    private long unitsInStock;
    private long uintsInOrder;
    private boolean discountinued;
//getter and setter

现在当我尝试在jsp中获取值为:

   <h3>${product.name}</h3>
   <p>${product.description}</p>
   <p>${product.unitPrice}USD</p>
   <p>Available ${product.unitsInStock} units in stock</p>

的web.xml

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

的DispatcherServlet-servlet.xml中

<mvc:annotation-driven />
    <context:component-scan base-package="com.*" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

在浏览器中没有获得任何输出但是当我将映射更改为 / 而不是 / products loadOnStartUp为1以用于调度程序servlet时,一切都很好。 有没有办法在没有直接映射到dispatcherServlet的情况下获取值,好像我有更多@controller类然后IDK,如何管理它?

修改

我有两个控制器,一个是

@RequestMapping("/")
   public String welcome(Model model) {
    model.addAttribute("greeting", "Greetings of the day");
    model.addAttribute("tagline", "this is the tagline");
    return "welcome";
}

工作正常,当我使用此配置时无法在浏览器中输出但是当我更改为

@RequestMapping("/")
    public String list(Model model) {
        Product iphone = new Product("P1233", "iPhone 5s", new BigDecimal(500));
        iphone.setDescription("pple iPhone 5s smartphone with 4.00-inch 640x1136 display and 8-megapixel rear camera");
        iphone.setCategory("smart phone");
        iphone.setManfactuer("Apple");
        iphone.setUnitsInStock(1000);
        model.addAttribute("product", iphone);
        return "products";

    }

给我输出,但我想使用@RequestMapping("/products),怎么做?

2 个答案:

答案 0 :(得分:0)

您已配置ViewResolver?

例如:

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

答案 1 :(得分:0)

配置web.xml

<servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern>     

你需要配置注解扫描包路径,及对应的视图解析器

<context:component-scan base-package="com.group.springmvc.controller" /> <mvc:annotation-driven enable-matrix-variables="true"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean>